jQuery.parseJSON单引号vs双引号

编程入门 行业动态 更新时间:2024-10-27 02:22:51
本文介绍了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双引号

本文发布于:2023-10-10 11:30:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478539.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单引号   双引号   jQuery   parseJSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!