JSON.stringify 没有转义?
我正在使用`JSON.stringify?字符串化一个对象,但引号没有转义?我是否误解它应该转义引号?
这将输出到模板中,而不会转义任何引号:
{"console":{"free":false}}
I'm using `JSON.stringify? to stringify an object, but the quotes are not escaped? Am I misunderstanding that it's suppose to escape the quotes?
This is outputted into the template without any of the quotes being escaped:
{"console":{"free":false}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将对象字符串化两次就可以了
stringify the object twice does the trick
它不会转义字符,不,有
encodeURIComponent
可以实现这一点,您可以将它们一起使用,如encodeURIComponent(JSON.stringify(obj))
It doesn't escape characters, no, there's
encodeURIComponent
for that, and you can use them together, as inencodeURIComponent(JSON.stringify(obj))
属性名称周围的引号不应转义,只能转义字符串内的引号。你的 JSON 没问题:)
The quotes around property names are not supposed to be escaped, only quotes inside strings. Your JSON is fine :)
如果没有要检查的违规代码,我想知道是否发生了其他事情。作为测试...
输出:
{"test":"This is \"text\"."}
(<注意转义的双引号)http://jsfiddle.net/userdude/YVGbH/
Without the offending code to inspect, I'm wondering if something else is happening. As a test...
Outputs:
{"test":"This is \"text\"."}
(< Note the escaped double quotes)http://jsfiddle.net/userdude/YVGbH/
这有点旧,但这是我的解决方案
结果
This is a bit old but here is my solution
result