-1
{
  "a" : "Test",
  "b" : "Got you", /** This data is easily parsable*/
  "c" : "Hello \"Test\" there is a problem" /** Not able to parse this data */
}

I need to parse a data with double quotes inside the value of a json object.

I tried JSONObject messageJson = new JSONObject(jsonString); it unescapes the "\"" and converts it to " " ". So it isn't helping me. Also I used ObjectMapper from jackson which couldn't map the value to the pojo object due to it " \" ".

distractor
  • 29
  • 5
  • *"Not able to parse"* - what's the exact problem, error message, stack trace? Which JSON library are you using? – kryger Mar 08 '18 at 10:34
  • What did you try? – PJProudhon Mar 08 '18 at 10:59
  • I tried JSONObject messageJson = new JSONObject(jsonString); it unescapes the "\"" and converts it to " " ". So it isn't helping me. Also I used ObjectMapper from jackson which couldn't map the value to the pojo object due to it " \" ". – distractor Mar 08 '18 at 13:40
  • @distractor edit the original question and include *all* the code you're running, don't *describe* your code in the comments. – kryger Mar 08 '18 at 15:11
  • @distractor you're still trying hard to prevent anyone from helping you. What is "jsonString" in your code? How is it declared? Where are the stack traces? If you can't be bother including this basic information, why even post a question? – kryger Mar 09 '18 at 09:03

1 Answers1

-1

You escape it with backslash. {"Attribute_1":"\"test\"" and that JSON is invalid.

Anyway the problem is that when it has reached {"Attribute_1":"", the parser thinks it has read a key-value pair and complains that what follows isn't either a , or a }

Solution: You have to provide the json accordingly, so the compiler can escape the "" for json value. Write the json String as:

String json = "{\"a\" : \"Test\",\"b\" : \"Got you\",\"c\" : \"Hello \\\"Test\\\" there is a problem\"}";

Sample Test Code:

public static void main(String[] args) throws Exception {
    TypeReference<HashMap<String, String>> tr = new TypeReference<HashMap<String, String>>() {
    };
    String json = "{\"a\" : \"Test\",\"b\" : \"Got you\",\"c\" : \"Hello \\\"Test\\\" there is a problem\"}";
    System.out.println(new ObjectMapper().readValue(json, tr));
}
Gaurav Tyagi
  • 641
  • 7
  • 7
  • 1
    OP already escaped the quotes inside string literals. The JSON (once comments are removed) is valid, there's obviously not enough information in the question to attempt to answer it. – kryger Mar 08 '18 at 11:51
  • 1
    ...and you stole comments from [another](https://stackoverflow.com/questions/46525369/java-jackson-objectmapper-fails-when-value-contains-double-quotes/49172223#comment80004621_46525369) [answer](https://stackoverflow.com/questions/46525369/java-jackson-objectmapper-fails-when-value-contains-double-quotes/49172223#comment80004698_46525369) without even checking if they fit here at all? – kryger Mar 08 '18 at 12:10
  • That's what I had explained that the escaping was incorrect in java, and it's not stole or something, I just gave a reference. – Gaurav Tyagi Mar 09 '18 at 03:47