为什么JSON只允许将字符串作为键? [英] Why does JSON only allow a string to be a key?
问题描述
为什么JSON只允许string
成为pair
的键?为什么不使用其他类型,例如null
,number
,bool
,object
,array
?考虑到JSON与JavaScript紧密相关,我可以从JavaScript规范(ECMA-262)中得出结论吗?我完全是JavaScript的新手,您能帮我指出一下吗?
也许使用null
和bool
并不明智,但是我认为number
和object
可以.就像C#中的Hashtable
一样,它允许我将几乎所有内容存储为键.我想在某些情况下不仅将通用类型用于键,而且还将string
用作键. JSON中的值是通用的,为什么不输入键.
谢谢!
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.
Maybe it''s not wise using null
and bool
, but I think number
and object
are OK. Just like Hashtable
in C# which allows me to store almost anything as a key. I''d like to use generic types for key not only string
in some situation; value in JSON is generic, why not key.
Thanks!
推荐答案
如果您只是在这里 http://www.json.org / [ ^ ],您将看到对象的定义是第一个语法图. JSON规范的一部分是名称/值对的名称部分始终是字符串.
每个定义的事实.
问候,
曼弗雷德(Manfred)
If you just go here http://www.json.org/[^] you''ll see the definition of object is the first syntax diagramm. It is part of the JSON specification that the name part of a name/value pair is always a string.
Factum per definitionem.
Regards,
Manfred
这篇关于为什么JSON只允许将字符串作为键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!