使用单引号和双引号时,JSON Key有什么区别吗? [英] Is there any difference in JSON Key when using single quote and double quote?

查看:268
本文介绍了使用单引号和双引号时,JSON Key有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在在线JS运行平台上运行了两段javascript代码:网站链接

I ran two pieces of javascript codes in a online JS running platform:Website Link

pets = '{'pet_names':[{"name":"jack"},{"name":"john"},{"name":"joe"}]}';
var arr = JSON.parse(pets);
alert(arr.pet_names[1].name);

带双引号(pet_names)的代码可以正常但有单引号('pet_names')会提醒错误:意外的标识符

Code with double quotes ("pet_names") would be OK but with single quotes('pet_names') would remind a error:"Unexpected identifier"

pets = '{"pet_names":[{"name":"jack"},{"name":"john"},{"name":"joe"}]}';
var arr = JSON.parse(pets);
alert(arr.pet_names[1].name);

那么,为什么会这样呢?

So, why do it would happen?

推荐答案

在JSON中,只有双引号有效。

In JSON only double quotes are valid.

您可以在 JSON.org


值可以是中的字符串双引号 ,或数字,或true或
false或null,或对象或数组。这些结构可以是
嵌套。

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.

换句话说,单引号中没有字符串。

In other words, no strings in single quotes.

这篇关于使用单引号和双引号时,JSON Key有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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