问题描述
我使用的是发出请求到服务器 jQuery.post()
和我的服务器返回的JSON对象(如 {变种: 值,...}
。但是,如果任何一个值,包含一个单引号(正确转义像 \
),jQuery的失败解析一个非常有效的JSON字符串这里是我的意思(在Chrome的控制台做)的一个例子:
I'm making requests to my server using jQuery.post()
and my server is returning JSON objects (like { "var": "value", ... }
. However, if any of the values contains a single quote (properly escaped like \'
), jQuery fails to parse an otherwise valid JSON string. Here's an example of what I mean (done in Chrome's Console):
这是正常的吗?有没有办法通过JSON正确地传递一个单引号?
Is this normal? Is there no way to properly pass a single quote via JSON?
推荐答案
据有关 JSON网站状态机图,只逃脱双引号字符是允许的,不是单引号。单引号字符不需要进行转义:
According to the state machine diagram on the JSON website, only escaped double-quote characters are allowed, not single-quotes. Single quote characters do not need to be escaped:
更新 - 对于那些有兴趣的更多信息:
Update - More information for those that are interested:
道格拉斯·克罗克福德并没有具体说出为什么JSON规范不允许转义字符串中的单引号。然而,在他的 JavaScript的附录E:好的部分的,他写道:
Douglas Crockford does not specifically say why the JSON specification does not allow escaped single quotes within strings. However, during his discussion of JSON in Appendix E of JavaScript: The Good Parts, he writes:
JSON的设计目标是微乎其微,便携式,文本和JavaScript的一个子集。我们需要达成一致,以实现互操作越少,越容易就可以实现互操作。
因此,也许,他决定只允许使用双引号,因为这是一个都不能少,所有的JSON的实现都必须同意的规则来定义字符串。其结果,这是不可能的一字符串中一个单引号字符意外终止串,因为根据定义,一个串只能由一个双引号字符终止。因此,没有必要让在正式的规范逃逸单引号的。
So perhaps he decided to only allow strings to be defined using double-quotes since this is one less rule that all JSON implementations must agree on. As a result, it is impossible for a single quote character within a string to accidentally terminate the string, because by definition a string can only be terminated by a double-quote character. Hence there is no need to allow escaping of a single quote character in the formal specification.
挖一点点更深,克罗克福德的 org.json 以Java实现的JSON是更允许和确实的允许单引号字符:
Digging a little bit deeper, Crockford's org.json implementation of JSON for Java is more permissible and does allow single quote characters:
用的toString方法产生的文本严格遵循JSON语法规则。构造函数是更宽容的文本,他们将接受:
...
- 在字符串可以用'(单引号)引用。
这是由 JSONTokener 源$ C $ C证实。该 nextString
方法接受转义单引号和对待他们就像双引号字符:
This is confirmed by the JSONTokener source code. The nextString
method accepts escaped single quote characters and treats them just like double-quote characters:
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;
...
在方法的顶部是一个内容的评论:
At the top of the method is an informative comment:
正式JSON格式不允许在单引号的字符串,而是一个实现允许接受它们。
因此,一些实施将接受单引号 - 但你不应该依赖于这一点。许多流行的实现是非常严格在这方面并拒绝JSON包含单引号字符串和/或转义单引号。
So some implementations will accept single quotes - but you should not rely on this. Many popular implementations are quite restrictive in this regard and will reject JSON that contains single quoted strings and/or escaped single quotes.
最后,以配合该回到原来的问题, jQuery.parseJSON
使用浏览器的原生JSON解析器或加载库的第一次尝试,如的在适用情况下(这在一个侧面说明是jQuery的逻辑是基于如果 JSON库
没有定义)。因此,jQuery的只能是作为许可作为底层的实现:
Finally to tie this back to the original question, jQuery.parseJSON
first attempts to use the browser's native JSON parser or a loaded library such as json2.js where applicable (which on a side note is the library the jQuery logic is based on if JSON
is not defined). Thus jQuery can only be as permissive as that underlying implementation:
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的。
As far as I know these implementations only adhere to the official JSON specification and do not accept single quotes, hence neither does jQuery.
这篇关于在JSON响应jQuery的单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!