javascript - jQuery.parseJSON 单引号与双引号

标签 javascript jquery json

这到底有什么区别?

这很好用:

var obj1 = jQuery.parseJSON('{"orderedList": "true"}');
document.write("obj1 "+ obj1.orderedList );

但以下不起作用:

var obj2 = jQuery.parseJSON("{'orderedList': 'true'}");
document.write("obj2 "+ obj2.orderedList );

这是为什么呢?

最佳答案

这是因为双引号被认为是标准的,而单引号不是。这并不是 JQuery 所特有的,而是关于 JSON standard .因此,无论 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. These structures can be nested.

更新

或者它可能是 jQuery single quote in JSON response 的副本

关于javascript - jQuery.parseJSON 单引号与双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14355655/

相关文章:

javascript - 使用 async wait 从多个 api 获取数据

javascript - 将具有多个分隔符的字符串拆分为 JavaScript 中的哈希表

javascript - jquery - 获取元素的出现

javascript - 如何访问嵌套的 JSON 数据

javascript - 从 JSON 模式生成 JavaScript 类

javascript - 如何保留在 node_modules 中所做的更改?

javascript - intro.js 是如何工作的?

javascript - Google Apps 脚本 - XML 解析器 - 正则表达式

javascript - 将输入选择作为文本输出到元素

java - 如何使用 google-Gson 和 JSON.stringify 获得相同的结果