为什么JSON只允许将字符串作为键? [英] Why does JSON only allow a string to be a key?

查看:70
本文介绍了为什么JSON只允许将字符串作为键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么JSON只允许string 成为pair的键?为什么不使用其他类型,例如nullnumberboolobjectarray?考虑到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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆