将此 JSON 对象转换为 java 中的类,您的 POJO 类中的映射将如何?
{
"ownerName": "Robert",
"pets": [
{
"name": "Kitty"
},
{
"name": "Rex"
},
{
"name": "Jake"
}
]
}
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 http://www.jsonschema2pojo.org/. Example print screen shows how to use it:
如何使用它:
- 选择目标语言。
Java
在你的情况下。
- 选择来源。
JSON
在你的情况下。
- 选择注释样式。这可能很棘手,因为它取决于您想要用于序列化/反序列化的库
JSON
。如果模式很简单,请不要使用注释(None
选项)。
- 选择其他可选配置选项,例如
Include getters and setters
。你可以在你的IDE
以及。
- Select
Preview
按钮。如果架构很大,请下载ZIP
与生成的类。
为您JSON
该工具生成:
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;
}
}
For Android Studio
and Kotlin
read RIP http://www.jsonschema2pojo.org https://medium.com/@rameshprasad/rip-http-www-jsonschema2pojo-org-fda47c0a95d0.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)