开发者

net.sf.json.JSONObject adds backslashes where it's not expected?

开发者 https://www.devze.com 2023-01-01 10:40 出处:网络
I\'m loosing my 开发者_如何学编程hair trying to figure out why net.sf.json.JSONObject add extra backslash where it shouldn\'t on my java code :

I'm loosing my 开发者_如何学编程hair trying to figure out why net.sf.json.JSONObject add extra backslash where it shouldn't on my java code :

JSONObject obj = new JSONObject ();
obj.element ("column_name", "<a href=\"#\" title=\"test\">Test !</a>");

will output :

<a href=\"#\" title=\"test\">Test !<\/a>

with an extra "\" near </a>.

How can I avoid this behavior ?


It probably uses the same method to escape strings in JSON as it does JavaScript strings used in script blocks (<script ...> ... </script>) which according to HTML syntax rules may not include the character sequence </.

Does this make any difference to you? Escaping "random" characters doesn't change the meaning of a string literals in JSON or JavaScript. The string literals "/" and "\/" are technically absolutely identical:

if ("/" === "\/") alert("The same!");

EDIT: BTW, the JSON grammer explicitly lists the forward slash (solidus) as an character that can be escaped.


I also ran into this issue. Turns out the problem was that my code used both org.json.JSONObject and com.amazonaws.util.json.JSONObject. The conversion between the two was creating strings and escape characters. Considering you're using net.sf.json.JSONObject, this might be your issue as well. Check your imports for other versions of JSONObject.

0

精彩评论

暂无评论...
验证码 换一张
取 消