Java 对象列表的 JSON 结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3916123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON Structure for List of Objects
提问by Vanchinathan Chandrasekaran
I would like to know, whats the right structure for a list of objects in JSON.
我想知道,JSON 中对象列表的正确结构是什么。
We are using JAXB to convert the POJO's to JSON.
我们正在使用 JAXB 将 POJO 转换为 JSON。
Here is the choices, Please direct me what is right.
这是选择,请指导我什么是正确的。
foos: [
foo:{..},
foo:{..}
]
or
或者
foos : [
{...},
{...}
]
If the first structure is right, what is the JAXB annotation I should use to get the structure right.
如果第一个结构是正确的,那么我应该使用什么 JAXB 注释来使结构正确。
采纳答案by BalusC
The first one is invalid syntax. You cannot have object properties inside a plain array. The second one is right although it is not strictJSON. It's a relaxedform of JSON wherein quotes in string keys are omitted.
第一个是无效的语法。普通数组中不能有对象属性。第二个是正确的,尽管它不是严格的JSON。这是一种轻松的 JSON 形式,其中省略了字符串键中的引号。
This tutorial by Patrick Hunlock, may help to learn about JSON and this sitemay help to validate JSON.
Patrick Hunlock 的本教程可能有助于了解 JSON,并且该站点可能有助于验证 JSON。
回答by Justin Niessner
The second is almost correct:
第二个几乎是正确的:
{
"foos" : [{
"prop1":"value1",
"prop2":"value2"
}, {
"prop1":"value3",
"prop2":"value4"
}]
}
回答by Timothy Kanski
As others mentioned, Justin's answer was close, but not quite right. I tested this using Visual Studio's "Paste JSON as C# Classes"
正如其他人所提到的,贾斯汀的回答很接近,但并不完全正确。我使用 Visual Studio 的“将 JSON 粘贴为 C# 类”对此进行了测试
{
"foos" : [
{
"prop1":"value1",
"prop2":"value2"
},
{
"prop1":"value3",
"prop2":"value4"
}
]
}