JSON密钥是否必须用引号引起来? [英] Do the JSON keys have to be surrounded by quotes?

查看:118
本文介绍了JSON密钥是否必须用引号引起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例: 以下代码对 JSON规范有效吗?

Example: Is the following code valid against the JSON Spec?

{
    precision: "zip"
}

还是我应该始终使用以下语法? (如果是这样,为什么?)

Or should I always use the following syntax? (And if so, why?)

{
    "precision": "zip"
}

我还没有在JSON规范中真正找到有关此的东西.尽管他们在示例中使用了双引号.

I haven't really found something about this in the JSON specifications. Although they use quotes around their keys in their examples.

推荐答案

是的,您需要引号.这是为了使其更简单,并且避免了必须为JavaScript保留关键字使用另一种转义方法,即{for:"foo"}.

Yes, you need quotation marks. This is to make it simpler and to avoid having to have another escape method for javascript reserved keywords, ie {for:"foo"}.

这篇关于JSON密钥是否必须用引号引起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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