jQuery.parseJSON单引号vs双引号 [英] jQuery.parseJSON single quote vs double quote

查看:453
本文介绍了jQuery.parseJSON单引号vs双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这实际上有什么区别?



这样可行:

  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双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆