Escape character in JSON analysis with org.json.JSONObject;

I use org.json to parse and write json. When serialized, i.e. When converting to a string, I see that the json object adds an extra escape character. How can this be avoided if possible?

String jsonStr = "{\"AD\":\"</p>\"}"; JSONObject jsonObject = new JSONObject(jsonStr); System.out.println(jsonStr); System.out.println(jsonObject.toString()); 

Output:

 {"AD":"</p>"} {"AD":"<\/p>"} 
+4
source share
1 answer

Several other StackOverflow posts indicate that this is because (1) is allowed by the JSON specification, and (2) it allows you to insert a JSON as-is string into specific XML / HTML contexts that would otherwise not allow strings with "</" inside them.

If this causes problems, I would look for the Best Java JSON Library - one that allows you to define additional character escaping options.

0
source

All Articles