我正在使用 jQuery.post()
向我的服务器发出请求,而我的服务器正在返回 JSON 对象(如 { "var": "value", ... }
)。但是,如果任何值包含单引号(正确转义,如 \'
),jQuery 将无法解析其他有效的 JSON 字符串。这是我的意思的一个例子(done in Chrome’s console):
data = "{ \"status\": \"success\", \"newHtml\": \"Hello \\\'x\" }";
eval("x = " + data); // { newHtml: "Hello 'x", status: "success" }
$.parseJSON(data); // Invalid JSON: { "status": "success", "newHtml": "Hello \'x" }
这正常吗?有没有办法通过 JSON 正确传递单引号?
最佳答案
根据 JSON website 上的状态机图, 只允许转义的双引号字符,而不是单引号。单引号字符不需要转义:
更新 - 为感兴趣的人提供更多信息:
Douglas Crockford 没有具体说明为什么 JSON 规范不允许在字符串中转义单引号。然而,在他在 Appendix E of JavaScript: The Good Parts 中讨论 JSON 时,他写道:
JSON's design goals were to be minimal, portable, textual, and a subset of JavaScript. The less we need to agree on in order to interoperate, the more easily we can interoperate.
所以也许他决定只允许使用双引号定义字符串,因为这是所有 JSON 实现都必须同意的规则。因此,字符串中的单引号字符不可能意外终止字符串,因为根据定义,字符串只能由双引号字符终止。因此,在正式规范中没有必要允许转义单引号字符。
再深入一点,Crockford 的 org.json Java 的 JSON 实现更允许,确实允许单引号字符:
The texts produced by the toString methods strictly conform to the JSON syntax rules. The constructors are more forgiving in the texts they will accept:
...
- Strings may be quoted with ' (single quote).
JSONTokener 证实了这一点源代码。 nextString
方法接受转义的单引号字符并将它们视为双引号字符:
public String nextString(char quote) throws JSONException {
char c;
StringBuffer sb = new StringBuffer();
for (;;) {
c = next();
switch (c) {
...
case '\\':
c = this.next();
switch (c) {
...
case '"':
case '\'':
case '\\':
case '/':
sb.append(c);
break;
...
方法的顶部是一条信息性注释:
The formal JSON format does not allow strings in single quotes, but an implementation is allowed to accept them.
所以一些实现会接受单引号 - 但你不应该依赖这个。许多流行的实现在这方面非常严格,并且会拒绝包含单引号字符串和/或转义单引号的 JSON。
最后把这个和原来的问题联系起来,jQuery.parseJSON
首次尝试使用浏览器的原生 JSON 解析器或加载的库,例如 json2.js在适用的情况下(如果未定义 JSON
,则附带说明 jQuery 逻辑所基于的库)。因此 jQuery 只能与底层实现一样宽松:
parseJSON: function( data ) {
...
// Attempt to parse using the native JSON parser first
if ( window.JSON && window.JSON.parse ) {
return window.JSON.parse( data );
}
...
jQuery.error( "Invalid JSON: " + data );
},
据我所知,这些实现只遵循官方 JSON 规范,不接受单引号,因此 jQuery 也不接受。
关于javascript - 由于 JSON 中的转义单引号,jQuery.parseJSON 抛出 “Invalid JSON” 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2275359/