简体   繁体   中英

JSON Escaping double quotes not working

I'm getting errors on a screen when it's trying to eval some json, it's giving me the error unexpected identifier..

The data causing the issue and how it's coming back when I check the Json response is:

"itemDescription":"STANDARD \\"B\\" RED BOX",

I'm using the below code in the java to handle the double quotes:

itemDescription = itemDescription.replaceAll("\\r|\\n", "");
itemDescription = itemDescription.replaceAll("\"", "\\\\\"");
itemDescription = itemDescription.replaceAll("'", "'");

Any idea why this wouldn't be working? If I remove the double quotes, I no longer get any errors.

Item descriptions such as "itemDescription":"STANDARD 16\\" RED BOX" go through fine..

Thanks!

You need two more backslashes at line 2:

itemDescription = itemDescription.replaceAll("\"", "\\\\\\\"").

So that " is replaced by \\\\\\" and not \\\\" .

尝试使用StringEscapeUtils

itemDescription  = StringEscapeUtils.escapeJson(itemDescription);

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