[英]jQuery.parseJSON single quote vs double quote
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 . 这不是JQuery特有的,而是JSON标准 。 So irrespective of JS toolkit, you should expect same behaviour. 因此,无论JS工具包如何,您都应该期待相同的行为。
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. 值可以是双引号中的字符串,也可以是数字,或者true或false或null,或者是对象或数组。 These structures can be nested. 这些结构可以嵌套。
Update 更新
Or perhaps its a duplicate of jQuery single quote in JSON response 或者它可能是JSON响应中jQuery单引号的副本
As per the API documentation, double quotes are considered valid JSON, single quotes aren't. 根据API文档,双引号被认为是有效的JSON,单引号则不是。
http://api.jquery.com/jQuery.parseJSON/ http://api.jquery.com/jQuery.parseJSON/
Go to www.Jsonlint.com website and check your single quotes json string you will found that it is not a valid json string. 访问www.Jsonlint.com网站并查看单引号json字符串,您将发现它不是有效的json字符串。 Because double quotes json is standard json format. 因为双引号json是标准的json格式。
jsonlint.com is a website to check json format right or not. jsonlint.com是一个检查json格式是否合适的网站。
You could use replace to fix this. 您可以使用替换来解决此问题。 This worked for me. 这对我有用。
var str = "{'orderedList': 'true'}";
str = str.replace(/\'/g, '\"');
JSON.parse(str);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.