简体   繁体   中英

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

I am using org.json to parse and write json. While serializing, ie converting to string, I see json object adds an extra escape character. How can be 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>"}

A number of other StackOverflow posts point out that this happens because (1) it is allowed by the JSON spec, and (2) it allows the JSON string to be inserted as-is into certain XML/HTML contexts that would otherwise not allow strings with "</" inside them.

If this causes problems, I would seek out A Better Java JSON Library --one that lets you define more character-escaping options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM