1

我在 JSON 对象中有一个数组属性。当数据在数组中有多个元素时,这些数组显示方括号'[]'。但是如果数据数组中有一个元素,它会在 JSON 中显示大括号'{}'。那么有没有更好的解决方案来使用方括号'[ ]'当只有单个元素时?我正在使用 CXF 框架。

或者

当只有单个元素存在时,大括号“{}”和没有方括号的原因是什么?我正在使用 CXF 框架。

4

2 回答 2

0

将 json 对象中的数组视为 json 对象中的第二个 json 对象。你得到的是例如这个单一的 json-'array'

{
    "this": "is a single object",
    "another" : "object in a json array"
}

请参阅填充了第二个 json 对象的“另一个”部分。

{
    "this": "is a single object",
    "although": [
        {
            "this": "is an object",
            "and this": "another object in a second object",
            "and this": "another object in a second object"
        }
    ]
}

在第二种情况下,“虽然”是这个 json 对象。

{
      "this": "is an object",
      "and this": "another object in a second object",
      "and this": "another object in a second object"
}

只是为了让 json 知道它是一个数组(另一个 json 对象),有[and ]

于 2012-04-26T13:39:54.367 回答
0

即使你有单个元素仍然使用这样的方括号:

[{"somename":"somevalue"}]

方括号用于分隔元素,因此如果只有一个元素,您可以使用大括号

于 2012-04-26T13:28:34.090 回答