Question

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 ?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top