是否有任何实际理由对 JSON 键使用带引号的字符串?

发布于 2024-10-03 14:17:41 字数 532 浏览 0 评论 0原文

根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帝王念 2024-10-10 14:17:41

JSON 键应该用引号引起来的真正原因依赖于 ECMAScript 3 标识符的语义。

保留字不能用作不带引号的对象文字中的属性名称,例如:

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

虽然如果使用引号,则属性名称有效:

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

自己的 Crockford 对此进行了解释在本次演讲中,他们希望保持 JSON 标准简单,并且不希望对其进行所有这些语义限制:

...

就在那时我们发现
未引用的名称问题。事实证明
ECMA Script 3 保留了一个重击
词政策。保留字必须是
在关键位置引用,即
真的很麻烦。当我到达周围时
为了将其制定为标准,我
不想把所有的
标准中的保留字,
因为它看起来真的很愚蠢。

当时我正试图说服
人们:是的,你可以写
JavaScript 中的应用程序,它是
真的去上班了,这很好
语言。那时我不想说,
同时:看看这个
他们确实做了愚蠢的事情!所以我
决定,相反,让我们引用
键。
这样我们就不用告诉
任何人都知道它有多糟糕。

这就是为什么直到今天,键都被引用在
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:

({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).

眼眸 2024-10-10 14:17:41

是的,它是无效的 JSON,否则在很多情况下都会被拒绝,例如 jQuery 1.4+ 有一个检查,可以使未加引号的 JSON 默默地失败。为什么遵守规定?

让我们再举一个例子:

{ myKey: "value" }
{ my-Key: "value" }
{ my-Key[]: "value" }

...所有这些带有引号是有效的,为什么不保持一致并在所有情况下使用它们,从而消除出现问题的可能性?

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:

{ myKey: "value" }
{ my-Key: "value" }
{ my-Key[]: "value" }

...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.

打小就很酷 2024-10-10 14:17:41

如果我正确理解了标准,那么 JSON 所谓的“对象”实际上更接近于地图(“字典”),而不是通常意义上的实际对象。当前的标准很容易容纳允许任何类型的键的扩展,从而形成

{
    "1" : 31.0,
      1 : 17,
     1n : "valueForBigInt1"
}

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

{
    "1" : 31.0,
      1 : 17,
     1n : "valueForBigInt1"
}

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).

子栖 2024-10-10 14:17:41

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文