gpt4 book ai didi

javascript - 在 JSON 中,为什么要引用每个名称?

转载 作者:IT老高 更新时间:2023-10-28 12:42:32 25 4
gpt4 key购买 nike

The JSON spec表示 JSON 是一个对象或数组。在对象的情况下,

An object structure is represented as a pair of curly bracketssurrounding zero or more name/value pairs (or members). A name is astring. ...

后来,规范说字符串用引号括起来。

为什么?

因此,

{"Property1":"Value1","Property2":18}

而不是

{Property1:"Value1",Property2:18}

问题 1:为什么不允许名称/值对中的名称为不带引号的标识符?


问题 2:在 Javascript 中评估时,上述两种表示形式之间是否存在语义差异?

最佳答案

我引用了 Douglas Crockford(JSON 标准的创建者)给雅虎的演示文稿。

他谈到了他是如何发现 JSON,以及为什么他决定使用引用键:

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

您可以找到完整的视频和文字记录 here .

关于javascript - 在 JSON 中,为什么要引用每个名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2067974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com