JSON 对象属性名称可以是整数吗?
我刚刚开始使用一些 Jackson JSON 数据。这部分给我带来了麻烦。
"pointData":{
"1":"32.1093904, 66.7065216",
"2":"33.1236854, 67.8128443",
"3":"32.9524550, 67.0013501"
}
在我看来,使用整数作为属性名称是非法的。这是正确的吗?
I am just getting started with some Jackson JSON data here. This section is giving me trouble.
"pointData":{
"1":"32.1093904, 66.7065216",
"2":"33.1236854, 67.8128443",
"3":"32.9524550, 67.0013501"
}
It seems to me that having integers as the attribute name is illegal. Is this correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是对的,JSON 不能具有整数属性名称,因为所有 JSON 属性名称都必须像上面那样用引号引起来,使它们成为字符串。请参阅此处的流程: http://json.org/
另外,上面的 JSON 结构是无效,因为它开始具有属性名称,但没有该属性所属的对象。如果您遇到错误,这就是原因。合法的结构是:
仅供参考,如果您要存储点数据,可能更好的结构是:
请注意有关此结构的两件事:
x
和y
> 可独立访问的财产。x
和y
属性的值是数字,而不是字符串。You're correct that JSON cannot have integer attribute names, because all JSON attribute names must be quoted as yours are above, making them strings. See the flow here: http://json.org/
Also, your JSON structure above is invalid because it begins with an attribute name, but no object that the attribute is a part of. If you're getting errors, this is why. A legal structure would be:
FYI, if you're storing point data, a perhaps better structure would be:
Notice two things about this structure:
x
andy
property that are independently accessible.x
andy
properties are numeric, not strings.这些不是整数,而是字符串。它们恰好是包含在其他上下文中也用于表示整数的字符的字符串,但它们仍然是字符串,因此这是有效的 JSON。来自 JSON 规范:
像这样的东西不会是有效的 JSON:
...因为这里的字符没有用双引号括起来,因此 JSON 中的键不是有效的。
Those are not integers, those are strings. They happen to be strings containing characters also used to represent integers in other contexts, but they are strings nonetheless and so this is valid JSON. From the JSON spec:
Something like this would not be valid JSON:
...because here the characters are not wrapped in double quotes, and so not valid keys in JSON.