javascript 为什么 JSON 只允许字符串作为键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9304528/
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
Why JSON allows only string to be a key?
提问by paladin_t
Why does JSON only allow a string
to be a key of a pair? Why not other types such as null
, number
, bool
, object
, array
? Considering JSON is tightly related with JavaScript, could I conclude the reason from JavaScript specification (ECMA-262)? I'm totally a newbie to JavaScript, could you help me to point it out.
为什么 JSON 只允许 astring
作为一对的键?为什么不是其他类型,例如null
, number
, bool
, object
, array
?考虑到 JSON 与 JavaScript 密切相关,我能否从 JavaScript 规范(ECMA-262)中得出原因?我完全是 JavaScript 的新手,你能帮我指出来吗?
回答by nnnnnn
The JSON format is deliberately based on a subset of JavaScript object literal syntax and array literal syntax, and JavaScript objects can only have strings as keys - thus JSON keys are strings too. (OK, you can sort of use numbers as JavaScript object keys, but really they get converted to strings.)
JSON 格式有意基于 JavaScript 对象字面量语法和数组字面量语法的子集,并且 JavaScript 对象只能将字符串作为键 - 因此 JSON 键也是字符串。(好吧,您可以将数字用作 JavaScript 对象键,但实际上它们会转换为字符串。)
Note that the point of JSON is that it is a string representation of data to allow easy exchange between programs written in different languages running on different machines in different environments. If you wanted to use an object as a key then that object would in turn have to be somehow represented as a string for transmission, but then the receiving language would need to be able to use objects as keys and that would mean you'd need a limited subset of JSON for those languages which would just be a mess.
请注意,JSON 的要点在于它是数据的字符串表示形式,以允许在不同环境中的不同机器上运行的不同语言编写的程序之间轻松交换。如果您想使用一个对象作为键,那么该对象又必须以某种方式表示为字符串以进行传输,但是接收语言需要能够使用对象作为键,这意味着您需要用于那些语言的 JSON 的有限子集,这只会是一团糟。
"Considering JSON is a part of JavaScript"
“考虑到 JSON 是 JavaScript 的一部分”
No, it isn't. Newer browsers provide methods for creating and parsing JSON, but they're not part of the language as such except that JSON is a string format and JavaScript can do strings. JSON is alwaysa string representation - it has to be parsed to create an object for use within JavaScript (or other languages) and once that happens JavaScript (or the other languages) treat the resulting object the same as any other object.
不,不是。较新的浏览器提供了用于创建和解析 JSON 的方法,但它们不是该语言的一部分,只是 JSON 是一种字符串格式并且 JavaScript 可以处理字符串。JSON始终是字符串表示形式 - 必须对其进行解析以创建在 JavaScript(或其他语言)中使用的对象,一旦发生这种情况,JavaScript(或其他语言)会将结果对象与任何其他对象一样对待。
(Note also that a particular bit of JSON doesn't necessarily have any keys at all: it could just be an array, like '["one","two","three"]'
.)
(另请注意,JSON 的特定位根本不一定有任何键:它可能只是一个数组,例如'["one","two","three"]'
.)
回答by Aparichith
Main reason according to the discoverer of JSON
representation is,
根据JSON
代表性的发现者的说法,主要原因是,
while parsing JSONdata, there is a chance/possibility that, keyyou are using to refer a valuemight be a reserved wordin your parsing language.
在解析JSON数据,有机会/可能性,关键你使用指一个值可能是 保留字在你的解析语言。
Refer this talkby Douglas Crockford, who is the discoverer of JSONrepresentation.
请参阅Douglas Crockford 的演讲,他是JSON表示的发现者。
Example :
例子 :
{
id: 1234,
name: "foo",
do: "somthing"
}
Since JSONis a cross language compatibility, We can use this data set in many languages. But, the word do
is a keyword in Javascript
. It will end up in syntax error while parsing.
由于JSON是一种跨语言兼容性,我们可以在多种语言中使用此数据集。但是,这个词do
是 中的关键字Javascript
。解析时最终会出现语法错误。
回答by alex
Because that is the way the specification was written.
因为这就是规范的编写方式。