有没有办法使用引用的字符串为JSON钥匙任何实际的原因是什么? [英] Is there any practical reason to use quoted strings for JSON keys?

查看:233
本文介绍了有没有办法使用引用的字符串为JSON钥匙任何实际的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据Crockford的 json.org ,一个JSON的对象的由的成员的,这是由

每对由一个的字符串的和的的,用的字符串的定义为:


  

一个字符串是零个或多个序列
  UNI code字,用双
  引号,用反斜杠。一个
  字符被重新presented作为一个单一的
  字符串。字符串是非常
  很像C或Java字符串。


但在实践中,大多数的程序员甚至不知道,一个JSON键应该用双引号括起来,因为大多数浏览器不需要使用双引号。

这有什么意义打扰你周围的JSON在双引号?

有效示例:

  {
  是keyName:34
}

相对于无效:

  {
   键名称:34
}


解决方案

为什么JSON键应该用引号括起来的真正原因,依赖ECMAScript中3的标识符的语义。

保留字不能作为的属性名的中对象常量不包括引号,例如:

 ({功能:0})//的SyntaxError
({如:0})//的SyntaxError
({真:0})//的SyntaxError
//等等...

而如果你使用引号的属性名称是有效的:

 ({功能:0})// OK
({如果:0})// OK
({真:0})// OK

在自己的克罗克福德解释了它在这次谈话,他们希望保持JSON标准简单,他们不喜欢有它所有的语义限制:


  

...


  
  

这是当我们发现
  不带引号的名称问题。原来,
  ECMA脚本3预留了一个重击
  字方针。保留字必须是
  报价在关键的位置,这是
  一个真正的滋扰。当我身边
  到formulizing成标准这一点,我
  不想必须把所有的
  在标准的保留字,
  因为它看起来很愚蠢。


  
  

当时,我正试图说服
  人:是的,你可以写
  在JavaScript应用程序,这是
  其实去工作,这是一个很好的
  语言。我不想说的话,
  在同一时间:看看这个
  真正愚蠢的事情,他们做到了!所以我
  决定,相反,我们只引述
  钥匙。结果
  这样,我们就不必说
  有关如何捶它是任何人


  
  

这就是为什么,为了这一天,钥匙在报价
  JSON。


  
  

...


ECMAScript的第5版标准在实施ES5修复了这个,现在可以不带引号使用甚至保留字,在这两个,对象文字和成员访问( obj.function 在ES5确定)。

只是为了记录在案,这个标准是由软件供应商实现的这些日子里,你可以看到浏览器包括在这此功能的兼容表(见的保留字的属性名称的)

According to Crockford's json.org, a JSON object is made up of members, which is made up of pairs.

Every pair is made of a string and a value, with a string being defined as:

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

But in practice most programmers don't even know that a JSON key should be surrounded by double quotes, because most browsers don't require the use of double quotes.

Does it make any sense to bother surrounding your JSON in double quotes?

Valid Example:

{
  "keyName" : 34
}

As opposed to the invalid:

{
   keyName : 34
}

解决方案

The real reason about why JSON keys should be in quotes, relies in the semantics of Identifiers of ECMAScript 3.

Reserved words cannot be used as property names in Object Literals without quotes, for example:

({function: 0}) // SyntaxError
({if: 0}) // SyntaxError
({true: 0}) // SyntaxError
// etc...

While if you use quotes the property names are valid:

({"function": 0}) // Ok
({"if": 0}) // Ok
({"true": 0}) // Ok

The own Crockford explains it in this talk, they wanted to keep the JSON standard simple, and they wouldn't like to have all those semantic restrictions on it:

....

That was when we discovered the unquoted name problem. It turns out ECMA Script 3 has a whack reserved word policy. Reserved words must be quoted in the key position, which is really a nuisance. When I got around to formulizing this into a standard, I didn't want to have to put all of the reserved words in the standard, because it would look really stupid.

At the time, I was trying to convince people: yeah, you can write applications in JavaScript, it's actually going to work and it's a good language. I didn't want to say, then, at the same time: and look at this really stupid thing they did! So I decided, instead, let's just quote the keys.
That way, we don't have to tell anybody about how whack it is.

That's why, to this day, keys are quoted in JSON.

...

The ECMAScript 5th Edition Standard fixes this, now in an ES5 implementation, even reserved words can be used without quotes, in both, Object literals and member access (obj.function Ok in ES5).

Just for the record, this standard is being implemented these days by software vendors, you can see what browsers include this feature on this compatibility table (see Reserved words as property names)

这篇关于有没有办法使用引用的字符串为JSON钥匙任何实际的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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