JSON, replace quotes and slashes, but by what?

后端 未结 4 830
礼貌的吻别
礼貌的吻别 2021-01-19 18:49

I have the ungrateful task to build a JSON String in Java, manually, without any framework, just a StringBuilder. I know this is bad, but it is only part of a prototype, I w

4条回答
  •  萌比男神i
    2021-01-19 19:12

    With Prototype's string.evalJSON(), commons-lang's StringEscapeUtils.escapeJavaScript method works fine, but with JQuery's jQuery.parseJSON(string), it doesn't work if the string value contains apostrophes which get escaped by the above mentioned method.

    The JSON string shouldn't escape the apostrophe (== single quote character), strictly speaking.

    So I use the StringEscapeUtils.escapeJava(String) method which does the same as escapeJavaScript except for the single-quote character.

    But it escapes the unicode characters into hex notation, which augments the length of the resulting script if the string contains many unicode characters.

    Maybe we should use some JSON-dedicated libraries such as net.sf.json.util.JSONUtils.quote(String)

提交回复
热议问题