是否有任何实际理由对 JSON 键使用带引号的字符串?
根据 Crockford 的 json.org,一个 JSON 对象 由 组成成员,由对组成。
每对都由一个字符串和一个值组成,字符串定义为:
字符串是零个或多个的序列 Unicode 字符,用双引号包裹 引号,使用反斜杠转义。一个 字符被表示为单个 字符串。一串很 很像 C 或 Java 字符串。
但实际上,大多数程序员甚至不知道 JSON 键应该用双引号引起来,因为大多数浏览器不需要使用双引号。
将 JSON 用双引号引起来有什么意义吗?
有效示例:
{
"keyName" : 34
}
与无效示例相反:
{
keyName : 34
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JSON 键应该用引号引起来的真正原因依赖于 ECMAScript 3 标识符的语义。
保留字不能用作不带引号的对象文字中的属性名称,例如:
虽然如果使用引号,则属性名称有效:
自己的 Crockford 对此进行了解释在本次演讲中,他们希望保持 JSON 标准简单,并且不希望对其进行所有这些语义限制:
ECMAScript 第五版标准修复了这个问题,现在在 ES5 实现中,甚至在对象文字和成员访问中都可以使用不带引号的保留字(
obj.function
在 ES5 中可以)。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:
While if you use quotes the property names are valid:
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:
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).是的,它是无效的 JSON,否则在很多情况下都会被拒绝,例如 jQuery 1.4+ 有一个检查,可以使未加引号的 JSON 默默地失败。为什么不遵守规定?
让我们再举一个例子:
...所有这些都带有引号是有效的,为什么不保持一致并在所有情况下使用它们,从而消除出现问题的可能性?
Web 开发人员世界中一个更常见的示例:在大多数浏览器中呈现的无效 HTML 示例数以千计……这是否可以减轻调试或维护的痛苦?完全不是,恰恰相反。
另外 @Matthew 在下面的评论中提出了最好的观点,这个已经失败了,不带引号的键将引发语法错误
JSON.parse()
在所有主要浏览器(以及正确实现它的任何其他浏览器)中,您可以在此处进行测试。Yes, it's invalid JSON and will be rejected otherwise in many cases, for example jQuery 1.4+ has a check that makes unquoted JSON silently fail. Why not be compliant?
Let's take another example:
...all of these would be valid with quotes, why not be consistent and use them in all cases, eliminating the possibility of a problem?
One more common example in the web developer world: There are thousands of examples of invalid HTML that renders in most browsers...does that make it any less painful to debug or maintain? Not at all, quite the opposite.
Also @Matthew makes the best point of all in comments below, this already fails, unquoted keys will throw a syntax error with
JSON.parse()
in all major browsers (and any others that implement it correctly), you can test it here.如果我正确理解了标准,那么 JSON 所谓的“对象”实际上更接近于地图(“字典”),而不是通常意义上的实际对象。当前的标准很容易容纳允许任何类型的键的扩展,从而形成
3 个不同元素的有效“对象/映射”。
如果不是因为这个原因,我相信设计者会对所有情况下可选的键进行引号(可能除了关键字)。
If I understand the standard correctly, what JSON calls "objects" are actually much closer to maps ("dictionaries") than to actual objects in the usual sense. The current standard easily accommodates an extension allowing keys of any type, making
a valid "object/map" of 3 different elements.
If not for this reason, I believe the designers would have made quotes around keys optional for all cases (maybe except keywords).
YAML 实际上是 JSON 的超集,支持您想要执行的操作。尽管它是一个超级组,但它可以让您按照自己的意愿保持简单。
YAML 令人耳目一新,也许值得您花时间看一下。最好的起点是这里:http://en.wikipedia.org/wiki/YAML
是世界上所有语言的库,包括 JS,例如 https://github.com/nodeca/js- yaml
YAML, which is in fact a superset of JSON, supports what you want to do. ALthough its a superset, it lets you keep it as simple as you want.
YAML is a breath of fresh air and it may be worth your time to have a look at it. Best place to start is here: http://en.wikipedia.org/wiki/YAML
There are libs for every language under the sun, including JS, eg https://github.com/nodeca/js-yaml