JSON 键是否必须用引号括起来?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/949449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do the JSON keys have to be surrounded by quotes?
提问by christianvuerings
Example: Is the following code valid against the JSON Spec?
示例:以下代码对JSON 规范有效吗?
{
precision: "zip"
}
Or should I always use the following syntax? (And if so, why?)
或者我应该总是使用以下语法?(如果是,为什么?)
{
"precision": "zip"
}
I haven't really found something about this in the JSON specifications. Although they use quotes around their keys in their examples.
我还没有在 JSON 规范中真正找到关于此的内容。尽管他们在示例中使用引号将键括起来。
采纳答案by cobbal
Yes, you need quotation marks. This is to make it simpler and to avoid having to have another escape method for javascript reserved keywords, ie {for:"foo"}.
是的,你需要引号。这是为了使其更简单,并避免必须为 javascript 保留关键字使用另一种转义方法,即{for:"foo"}.
回答by PatrikAkerstrand
You are correct to use strings as the key. Here is an excerpt from RFC 4627 - The application/json Media Type for JavaScript Object Notation (JSON)
使用字符串作为键是正确的。这是RFC 4627的摘录- JavaScript 对象表示法 (JSON) 的应用程序/json 媒体类型
2.2. Objects
An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. The names within an object SHOULD be unique.
object = begin-object [ member *( value-separator member ) ] end-object
member = string name-separator value[...]
2.5. Strings
The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks. [...]
string = quotation-mark *char quotation-mark
quotation-mark = %x22 ; "
2.2. 对象
对象结构表示为一对大括号,围绕零个或多个名称/值对(或成员)。 名称是一个字符串。每个名称后面都有一个冒号,将名称与值分开。单个逗号将值与以下名称分开。对象内的名称应该是唯一的。
object = begin-object [ member *( value-separator member ) ] end-object
member = string name-separator value[...]
2.5. 字符串
字符串的表示类似于 C 系列编程语言中使用的约定。字符串以引号开始和结束。[...]
string = quotation-mark *char quotation-mark
quotation-mark = %x22 ; "
Read the whole RFC here.
在此处阅读整个 RFC 。
回答by Cebjyre
From 2.2. Objects
从2.2. 对象
An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string.
对象结构表示为一对大括号,围绕零个或多个名称/值对(或成员)。名称是一个字符串。
and from 2.5. Strings
和从2.5。字符串
A string begins and ends with quotation marks.
字符串以引号开始和结束。
So I would say that according to the standard: yes, you should always quote the key (although some parsers may be more forgiving)
所以我会说,根据标准:是的,你应该总是引用键(尽管一些解析器可能更宽容)
回答by Cebjyre
回答by Inigo
Yes they do. But if you need otherwise, checkout JSON5.
是的,他们这样做。但如果您需要其他方式,请查看 JSON5。
JSON5is a superset of JSON that allows ES5 syntax, including:
JSON5是 JSON 的超集,允许使用 ES5 语法,包括:
- unquotedproperty keys
- single-quoted, escaped and multi-line strings
- alternate number formats
- comments
- extra whitespace
- 不带引号的属性键
- 单引号、转义和多行字符串
- 替代数字格式
- 注释
- 额外的空白
The JSON5 reference implementation (json5npm package) provides a JSON5object that has parseand stringifymethods with the same args and semantics as the built-in JSONobject.
JSON5 参考实现(json5npm 包)提供了一个JSON5对象,该对象具有parse与stringify内置JSON对象相同的参数和语义的方法。
回答by Master James
Since you can put "parent.child" dotted notation and you don't have to put parent["child"] which is also valid and useful, I'd say both ways is technically acceptable. The parsers all should do both ways just fine. If your parser does not need quotes on keys then it's probably better not to put them (saves space). It makes sense to call them strings because that is what they are, and since the square brackets gives you the ability to use values for keys essentially it makes perfect sense not to. In Json you can put...
由于您可以放置“parent.child”点号,而不必放置 parent["child"] 这也是有效且有用的,因此我认为这两种方式在技术上都是可以接受的。解析器都应该在两种方式下都很好。如果您的解析器不需要键上的引号,那么最好不要放置它们(节省空间)。将它们称为字符串是有道理的,因为它们就是这样,并且由于方括号使您能够使用键的值,因此不这样做是完全合理的。在 Json 中,您可以将...
>var keyName = "someKey";
>var obj = {[keyName]:"someValue"};
>obj
Object {someKey: "someValue"}
just fine without issues, if you need a value for a key and none quoted won't work, so if it doesn't, you can't, so you won't so "you don't need quotes on keys". Even if it's right to say they are technically strings. Logic and usage argue otherwise. Nor does it officially output Object {"someKey": "someValue"} for obj in our example run from the console of any browser.
很好没有问题,如果你需要一个键的值并且没有引用的值将不起作用,所以如果它没有,你不能,所以你不会所以“你不需要键上的引号”。即使说它们在技术上是字符串是正确的。逻辑和用法相反。在我们的示例中,它也没有正式为 obj 输出 Object {"someKey": "someValue"} 从任何浏览器的控制台运行。

