1

I have a JSON stored in a string.

String data = "{code: '0', distCode: '123'}";

I need to get the values of code, distCode. But when I try to parse it as below

JSONParser parser = new JSONParser();
JSONObject Details = (JSONObject) parser.parse(data);

Unexpected character (c) at position 2 exception is thrown.

I am sure it is because of unquoted keys in the string. How to parse the string into an JSON object using org.json.simple library?

gokul
  • 85
  • 11

1 Answers1

0

Could not find way to achieve it using org.json.simple library. Finally done it using jackson libraries.

    String data = "{code: '0', distCode: '123'}";
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);                               
    mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    Map<String, String> Shop_Details = mapper.readValue(data), Map.class);
gokul
  • 85
  • 11