JSON 对象属性名称可以是整数吗?

发布于 2024-12-10 18:08:43 字数 225 浏览 0 评论 0原文

我刚刚开始使用一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开浅夏 2024-12-17 18:08:43

您是对的,JSON 不能具有整数属性名称,因为所有 JSON 属性名称都必须像上面那样用引号引起来,使它们成为字符串。请参阅此处的流程: http://json.org/

另外,上面的 JSON 结构是无效,因为它开始具有属性名称,但没有该属性所属的对象。如果您遇到错误,这就是原因。合法的结构是:

{"pointData":{
    "1":"32.1093904, 66.7065216", 
    "2":"33.1236854, 67.8128443", 
    "3":"32.9524550, 67.0013501"
    }
}

仅供参考,如果您要存储点数据,可能更好的结构是:

{"pointData":{
    "1": {"x": 32.1093904, "y": 66.7065216}, 
    "2": {"x": 33.1236854, "y": 67.8128443}, 
    "3": {"x": 32.9524550, "y": 67.0013501}
    }
}

请注意有关此结构的两件事:

  1. 每个点都有一个 xy > 可独立访问的财产。
  2. xy 属性的值是数字,而不是字符串。

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:

{"pointData":{
    "1":"32.1093904, 66.7065216", 
    "2":"33.1236854, 67.8128443", 
    "3":"32.9524550, 67.0013501"
    }
}

FYI, if you're storing point data, a perhaps better structure would be:

{"pointData":{
    "1": {"x": 32.1093904, "y": 66.7065216}, 
    "2": {"x": 33.1236854, "y": 67.8128443}, 
    "3": {"x": 32.9524550, "y": 67.0013501}
    }
}

Notice two things about this structure:

  1. Each point has an x and y property that are independently accessible.
  2. The values of the x and y properties are numeric, not strings.
划一舟意中人 2024-12-17 18:08:43

这些不是整数,而是字符串。它们恰好是包含在其他上下文中也用于表示整数的字符的字符串,但它们仍然是字符串,因此这是有效的 JSON。来自 JSON 规范

字符串是零个或多个 Unicode 字符的序列,用双引号括起来,使用反斜杠转义。

像这样的东西不会是有效的 JSON:

{ 1:"32.1093904, 66.7065216", 
  2:"33.1236854, 67.8128443", 
}

...因为这里的字符没有用双引号括起来,因此 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:

A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes.

Something like this would not be valid JSON:

{ 1:"32.1093904, 66.7065216", 
  2:"33.1236854, 67.8128443", 
}

...because here the characters are not wrapped in double quotes, and so not valid keys in JSON.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文