是否有任何修改 JSON 以正式允许不带引号的属性名称的建议?

发布于 2024-08-14 08:30:32 字数 340 浏览 4 评论 0原文

以下内容在技术上是无效的 JSON:

{
  color: "blue",
  size: 14
}

因为规范要求引用属性名称“color”和“size”,即

{
  "color": "blue",
  "size": 14
}

但是,我注意到许多声称返回“JSON”的 Web 服务不引用其属性几乎没有人会引用它们的属性名称,因为 Javascript 不需要它。

是否有任何修改或分叉 JSON 以允许不带引号的属性名称的建议?这似乎是一种更自然的使用该格式的方式,但我从未见过任何建议应该正式采用它的内容。

The following is technically invalid JSON:

{
  color: "blue",
  size: 14
}

because the property names "color" and "size" are required by the spec to be quoted, i.e.

{
  "color": "blue",
  "size": 14
}

However, I've noticed that many web services that purport to return "JSON" do not quote their property names, and hardly anybody writing Javascript will quote their property names, since Javascript doesn't require it.

Have there been any proposals to amend or fork JSON to allow unquoted property names? It seems to be a much more natural way of using the format, but I've never seen anything suggesting it should be officially adopted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

谁与争疯 2024-08-21 08:30:32

JSON 的设计理念似乎是保持尽可能简单

为了简单起见,“用双引号引起来的属性名称”优于“用双引号或单引号引起来的属性名称,并且您可以保留引号,除非名称包含空格或其他特殊字符”。

我认为这不太可能改变。

现在,在我开始对 HTML5 设计哲学进行咆哮之前,我要停下来。

The philosophy of the design of JSON appears to be keep it as simple as possible.

"Wrap property names with double quotes" beats "Wrap property names with double quotes, or single quotes, and you can leave the quotes off unless the name includes spaces or other special characters" for simplicity.

I don't think that is likely to change.

Now I'm going to stop before I go off on a rant about HTML5 design philosophy.

夕色琉璃 2024-08-21 08:30:32

我在处理与 JavaScript 保留字冲突的属性名称时遇到了麻烦。在解决了几个这样的问题之后,当我有选择时,我会大量引用我的 JSON。在我看来,这样更安全。

I've had trouble with property names that collided with JavaScript reserved words. After fighting with a couple such problems, I keep my JSON heavily quoted when I have the choice. In my opinion, it's much safer.

药祭#氼 2024-08-21 08:30:32

不,我怀疑这会发生。如果您使用的 Web 服务声称返回 JSON,但这样做无效,请与他们联系。您不会修复规范,因为人们错误地实现了它。

我们不要在这里串通我们的条款。是的,JSON 的名称中包含“JavaScript”,但要充分理解 - JSON 不是 Javascript。

在你跳到我面前说“但是在 json.org 网站上它说 JSON 是 JavaScript 的子集” - 是的,我知道。

是的,从技术上讲是它的 JavaScript,但由于该格式是 a) 严格声明性的,b) 可在 JavaScript 上下文之外使用,因此最好以“不是”的心态来对待它 em> JavaScript。它只是一种数据格式,也恰好是原生 JavaScript。

这就是为什么您会注意到“JavaScript 不需要它”。嗯,那是因为 JavaScript 不支持,但 JSON 可以。这也是为什么我更喜欢将 JSON 称为“JavaScript,但不是真的”。

No, i doubt this will happen. If you are using a web-service that claim to return JSON but do-so invalidly, contact them. You don't fix a specification because people implement it wrong.

And let's not collude our terms, here. Yes, JSON has "JavaScript" in its name, but understand well - JSON is not Javascript.

And before you jump on me and say "But on the json.org site it says JSON is a subset of JavaScript" - yes, I know that.

Yes, technically speaking its JavaScript, but since the format is a) stricly declarative and b) usable outside of the context of JavaScript, it's best to treat it with the mindset that it's not JavaScript. It's just a data format that also happens to be native JavaScript.

Which is why you notice that "JavaScript doesn't require it". Well, that's because JavaScript doesn't, but JSON does. Which is again why I prefer to thing of JSON as "JavaScript, but not really".

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