简体   繁体   English

JSON.stringify没有逃脱?

[英]JSON.stringify doesn't escape?

I'm using `JSON.stringify? 我正在使用`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}}

它不会转义字符,不会有,它的encodeURIComponent ,你可以一起使用它们,如encodeURIComponent(JSON.stringify(obj))

stringify the object twice does the trick 对该对象进行两次stringify就可以了

console.log(JSON.stringify(JSON.stringify({"console":{"free":false}})));
// "{\"console\":{\"free\":false}}"

Without the offending code to inspect, I'm wondering if something else is happening. 没有违规代码进行检查,我想知道是否还有其他事情发生。 As a test... 作为测试......

<div id="test"/>

var ex = {'test':'This is "text".'};

$('#test').text(JSON.stringify(ex));

Outputs : {"test":"This is \\"text\\"."} (< Note the escaped double quotes) 输出{"test":"This is \\"text\\"."} (<注意转义的双引号)

http://jsfiddle.net/userdude/YVGbH/ http://jsfiddle.net/userdude/YVGbH/

The quotes around property names are not supposed to be escaped, only quotes inside strings. 属性名称周围的引号不应该被转义,只在字符串内引用。 Your JSON is fine :) 你的JSON很好:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM