0

The exception is Error on line 11, column 1: System.JSONException: Unexpected character ('n' (code 110)): was expecting comma to separate OBJECT entries at [line:1, column:36] Class.System.JSON.deserializeUntyped: line 11, column 1.

By running the following code. I expect {\"name\": \"c\"} could be treated as a normal string. But APEX just could parse this JSON. Any idea?

String structure = '{"name":"firstC", "structure": "{\"name\": \"c\"}" }';
Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(structure);
System.Debug(LoggingLevel.ERROR,  JSON.serializePretty(
m
));
3

JSON needs backslash characters to escape literal quotation marks, but backslash characters in Apex Code have to be escaped, so you have to double up on backslashes so that they will survive the initial compilation and make it to the JSON parser.

String structure = '{"name":"firstC", "structure": "{\\"name\\": \\"c\\"}" }';
Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(structure);

Note that this is a side effect of compilation; you do not need double-backslashes in JSON obtained from other sources, such as a file or from a HttpResponse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.