简体   繁体   English

JSON 键是否必须用引号括起来?

[英]Do the JSON keys have to be surrounded by quotes?

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.我还没有在 JSON 规范中真正找到关于此的内容。 Although they use quotes around their keys in their examples.尽管他们在示例中使用引号将键括起来。

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"}

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. 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. 2.5. Strings字符串

The representation of strings is similar to conventions used in the C family of programming languages.字符串的表示类似于 C 系列编程语言中使用的约定。 A string begins and ends with quotation marks.字符串以引号开始和结束。 [...] [...]

string = quotation-mark *char quotation-mark

quotation-mark = %x22 ; "

Read the whole RFC here .此处阅读整个 RFC。

From 2.2.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.名称是一个字符串。

and from 2.5.和从2.5。 Strings字符串

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)所以我会说,根据标准:是的,你应该总是引用键(尽管一些解析器可能更宽容)

Yes, quotes are mandatory.是的,引号是强制性的。 http://json.org/ says: http://json.org/说:

string
    ""
    " chars "

Yes they do.是的,他们这样做。 But if you need otherwise, checkout JSON5 .但如果您需要其他方式,请查看 JSON5

JSON5 is a superset of JSON that allows ES5 syntax, including: JSON5是 JSON 的超集,允许使用 ES5 语法,包括:

  • unquoted property keys不带引号的属性键
  • single-quoted, escaped and multi-line strings单引号、转义和多行字符串
  • alternate number formats替代数字格式
  • comments注释
  • extra whitespace额外的空白

The JSON5 reference implementation ( json5 npm package ) provides a JSON5 object that has parse and stringify methods with the same args and semantics as the built-in JSON object. JSON5 参考实现( json5 npm 包)提供了一个JSON5对象,该对象具有parsestringify方法,其参数和语义与内置JSON对象相同。

In your situation, both of them are valid, meaning that both of them will work.在您的情况下,它们都是有效的,这意味着它们都可以工作。

However, you still should use the one with quotation marks in the key names because it is more conventional , which leads to more simplicity and ability to have key names with white spaces etc.但是,您仍然应该使用在键名中带引号的那个,因为它更传统,这会导致更简单和使用空格等键名的能力。

Therefore, use the one with the quotation marks.因此,请使用带引号的那个。

edit// check this: What is the difference between JSON and Object Literal Notation?编辑// 检查这个: JSON 和对象文字符号有什么区别?

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.由于您可以放置​​“parent.child”点号,而不必放置 parent["child"] 这也是有效且有用的,因此我认为这两种方式在技术上都是可以接受的。 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...在 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"} 从任何浏览器的控制台运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM