Array of JSON Object to Java POJO(JSON对象数组到Java POJO)
问题描述
将此 JSON 对象转换为 java 中的类,映射将如何在您的 POJO 类中?
Converting this JSON object as a class in java, how would the mapping be in your POJO Class?
{
"ownerName": "Robert",
"pets": [
{
"name": "Kitty"
},
{
"name": "Rex"
},
{
"name": "Jake"
}
]
}
推荐答案
这种问题很流行,需要一般性的回答.如果您需要基于 JSON 或 JSON Schema 生成 POJO 模型,请使用
This kind of question is very popular and needs general answer. In case you need generate POJO model based on JSON or JSON Schema use www.jsonschema2pojo.org. Example print screen shows how to use it:
使用方法:
- 选择目标语言.
Java在你的情况下. - 选择来源.
JSON在你的情况下. - 选择注释样式.这可能很棘手,因为它取决于您要用于序列化/反序列化
JSON的库.如果架构很简单,请不要使用注释(None选项). - 选择其他可选配置选项,例如
包括 getter 和 setter.您也可以在IDE中执行此操作. - 选择
预览按钮.如果架构很大,请下载带有生成类的ZIP.
- Select target language.
Javain your case. - Select source.
JSONin your case. - Select annotation style. This can be tricky because it depends from library you want to use to serialise/deserialise
JSON. In case schema is simple do not use annotations (Noneoption). - Select other optional configuration options like
Include getters and setters. You can do that in yourIDEas well. - Select
Previewbutton. In case schema is big downloadZIPwith generated classes.
对于您的 JSON,此工具会生成:
For your JSON this tool generates:
public class Person {
private String ownerName;
private List <Pet> pets = null;
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public List < Pet > getPets() {
return pets;
}
public void setPets(List < Pet > pets) {
this.pets = pets;
}
}
public class Pet {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
对于 Android Studio 和 Kotlin 阅读 RIP http://www.jsonschema2pojo.org.
For Android Studio and Kotlin read RIP http://www.jsonschema2pojo.org.
这篇关于JSON对象数组到Java POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JSON对象数组到Java POJO
- 转换 ldap 日期 2022-01-01
- 获取数字的最后一位 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
