net.sf.json.JSONObject 在不需要的地方添加反斜杠?
我正在失去头发试图弄清楚为什么 net.sf.json.JSONObject 在我的 java 代码中不应该添加额外的反斜杠 :
JSONObject obj = new JSONObject ();
obj.element ("column_name", "<a href=\"#\" title=\"test\">Test !</a>");
将输出 :
<a href=\"#\" title=\"test\">Test !<\/a>
在 附近有一个额外的“\”。
我怎样才能避免这种行为?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它可能使用与在脚本块 (
) 中使用的 JavaScript 字符串相同的方法来转义 JSON 中的字符串,根据 HTML 语法规则不能包含字符序列
。
这对你有什么影响吗?转义“随机”字符不会改变 JSON 或 JavaScript 中字符串文字的含义。字符串文字
"/"
和"\/"
在技术上是完全相同的:编辑:顺便说一句,JSON 语法明确地将正斜杠(斜线)列为可以被逃脱。
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:EDIT: BTW, the JSON grammer explicitly lists the forward slash (solidus) as an character that can be escaped.
我也遇到了这个问题。事实证明,问题是我的代码同时使用了 org.json.JSONObject 和 com.amazonaws.util.json.JSONObject。两者之间的转换是创建字符串和转义字符。考虑到您正在使用 net.sf.json.JSONObject,这也可能是您的问题。检查导入的 JSONObject 的其他版本。
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.