对象列表的 JSON 结构

我想知道,JSON 中对象列表的正确结构是什么。

我们使用 JAXB 将 POJO 转换为 JSON。

这里是选择,请告诉我什么是正确的。

foos: [
foo:{..},
foo:{..}
]

或者

   foos : [
{...},
{...}
]

如果第一个结构是正确的,那么我应该使用什么样的 JAXB 注释来获得正确的结构。

396188 次浏览

The first example from your question,

foos: [
foo: { ... },
foo: { ... }
]

is in invalid syntax. You cannot have object properties inside a plain array.

The second example from your question,

foos: [
{ ... },
{ ... }
]

is right although it is not strict JSON. It's a relaxed form of JSON wherein quotes in string keys are omitted.

Following is the correct one when you want to obey strict JSON:

"foos": [
{ ... },
{ ... }
]

This "Mastering JSON" tutorial by Patrick Hunlock, may help to learn about JSON and this site may help to validate JSON.

The second is almost correct:

{
"foos" : [{
"prop1":"value1",
"prop2":"value2"
}, {
"prop1":"value3",
"prop2":"value4"
}]
}

As others mentioned, Justin's answer was close, but not quite right. I tested this using Visual Studio's "Paste JSON as C# Classes"

{
"foos" : [
{
"prop1":"value1",
"prop2":"value2"
},
{
"prop1":"value3",
"prop2":"value4"
}
]
}
  1. I use jsonformatter page to test the json validator.
  2. try to use jsonwhatever

example:

list_a = func_generator_of_objects()


json_string = jsonwhatever('foos',list_a)