1

How to convert this in Java using List or Hashmap?

{"mappingSet": ["key1": "value1", "key2": "value2"]}
3
  • Is the question here how to parse json into list or hashmap without any dependencies? Commented Jun 9, 2021 at 3:53
  • 2
    That isn't legal JSON, so I think you're going to have difficulties finding anything that would work. That array needs to be an object. If you really need to parse malformed JSON, you're going to have to do it by hand. Commented Jun 9, 2021 at 5:48
  • @Rohini The JSON is invalid. Commented Jun 9, 2021 at 6:08

1 Answer 1

1

you could use some third party lib like

  1. gson
  2. jackson

then you can covert json string to

  • pre-defined objects
  • lib built in objects like JsonObject(JsonArray, JsonElement) in 1.gson and JsonNode in 2.jackson

example here

    ObjectMapper objectMapper = new ObjectMapper();
    String jsonArray = "[{ \"color\" : \"Black\", \"type\" : \"FIAT\" }, { \"color\" : \"Green\", \"type\" : \"NIKE\" }]";
    JsonNode jsonNodeArray = objectMapper.readTree(jsonArray);
    System.out.println(jsonNodeArray);
    System.out.println(jsonNodeArray.get(0));
Sign up to request clarification or add additional context in comments.

6 Comments

inside the json array there is no json object.
by the way, I don't think your json string above is valid in format. json array could not contain key value pair
its a valid json.
@Rohini No, it isn't. You can't have key:value fields in an array. Run it past a JSON validator like JSONlint- it fails.
you can copy paste to any online json parser
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.