这实际上有什么区别?
这样可行:
var obj1 = jQuery.parseJSON('{orderedList:true}'); document.write(obj1+ obj1.orderedList);但以下情况不起作用:
var obj2 = jQuery.parseJSON({'orderedList':'true'}); document.write(obj2+ obj2.orderedList);为什么?
解决方案那是因为双引号被认为是标准而单引号则不是。这不是JQuery特有的,而是关于 JSON标准。因此,无论JS工具包如何,您都应该期望相同的行为。
值可以是双引号中的字符串,也可以是数字,或者为true或false或null,或对象或数组。这些结构可以嵌套。
更新
<或者它可能是 JSON响应中的jQuery单引号的副本What actually the difference between this?
This works fine:
var obj1 = jQuery.parseJSON('{"orderedList": "true"}'); document.write("obj1 "+ obj1.orderedList );but the following does not work:
var obj2 = jQuery.parseJSON("{'orderedList': 'true'}"); document.write("obj2 "+ obj2.orderedList );Why is that?
解决方案That's because double quotes is considered standard while single quote is not. This is not really specific to JQuery, but its about JSON standard. So irrespective of JS toolkit, you should expect same behaviour.
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
Update
Or perhaps its a duplicate of jQuery single quote in JSON response
更多推荐
jQuery.parseJSON单引号vs双引号
发布评论