包含转义单引号的 JSON 字符串被标记为无效
JSONLint 表示以下内容是无效的 JSON:
[{
"value": 71,
"label": "123 Foobar \'eha-Kauai, Hawaii, United States"
},
{
"value": 75,
"label": "456 Foobar \'elima-Kauai, Hawaii, United States"
}]
但这有效:
var foo = [{
"value": 71,
"label": "123 Foobar \'eha-Kauai, Hawaii, United States"
},
{
"value": 75,
"label": "456 Foobar \'elima-Kauai, Hawaii, United States"
}];
console.log(foo.length); // returns 2
注意:该字符串是由可以通过以下方式使用的函数生成的:
<script>
var foo = '<%= vbs_JSONEncode("Hello World") %>'; // being inconsistent
var bar = "<%= vbs_JSONEncode(str_Hello_Msg) %>"; // with the quotes
</script>
或者简单地:
Response.ContentType = "application/json"
Response.Write vbs_JSONEncode("Hello World")
JSONLint says the following is invalid JSON:
[{
"value": 71,
"label": "123 Foobar \'eha-Kauai, Hawaii, United States"
},
{
"value": 75,
"label": "456 Foobar \'elima-Kauai, Hawaii, United States"
}]
Yet this works:
var foo = [{
"value": 71,
"label": "123 Foobar \'eha-Kauai, Hawaii, United States"
},
{
"value": 75,
"label": "456 Foobar \'elima-Kauai, Hawaii, United States"
}];
console.log(foo.length); // returns 2
Note: the string is generated by a function which could be used in the following ways:
<script>
var foo = '<%= vbs_JSONEncode("Hello World") %>'; // being inconsistent
var bar = "<%= vbs_JSONEncode(str_Hello_Msg) %>"; // with the quotes
</script>
Or simply:
Response.ContentType = "application/json"
Response.Write vbs_JSONEncode("Hello World")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不需要使用反斜杠转义
'
。只需转义"
即可。You don't need to escape a
'
with a backslash. Just escape the"
.问题在于单引号的转义,这在 JSON 中是不必要的,因为只使用双引号。
The issue is the escaping of the single-quote marks, which is unnecessary in JSON, since only double quotes are used.
转义反斜杠?
发现 http://json.parser.online.fr/
Escape the backslashes?
Found with http://json.parser.online.fr/
由于使用了双引号,因此不需要转义 ' 。需要将其删除,然后 JSON 才有效。
There is no need for the ' to be escaped since double quotes are used. That needs to be removed and the JSON becomes valid.
在我看来,JSONLint 只是不太关心 JSON 中的
\
。我不知道它是否是 JSON 的无效字符。但正如 @dogbane 所说,不需要转义'
除非您使用该字符进行字符串封装。It looks to me like JSONLint just doesn't care too much for
\
inside your JSON. Wether or not it is an invalid character for JSON, i don't know.. But as @dogbane says, no need to escape'
unless you use that char for your string encapsulation..