views:

236

answers:

1

Hi there,

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 ?

Thanks for your help !

+1  A: 

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.

RoToRa
Well, that's odd, because I checked what you said and of course, you're right, but the problem is I add the html values in the dom into a table like element from extjs (grid).The column are broken (the data elements are not well verticaly aligned with the headers) and I found that the problem was coming from an invalid html. I checked the html returned by the request, and except the "\/", everything else is fine.It's very strange :pBut thanks for your help, I appreciate !
cx42net
You would need to give more details then, because right now I can't imagine why escaping the slashes would make problems, but escaping the quotes wouldn't.
RoToRa