使用JSON数据创建POJO [英] Create a POJO using JSON data
问题描述
我正在使用来自rest API的响应有效负载的JSON。下面是简化JSON的结构,但实际情况要复杂得多。
I am having a JSON coming from response payload of rest API. below is structure of simplified JSON but the actual is much more complex.
{
"hardware": {
"cores": 2,
"cpu": 1,
},
"name": "machine11",
"network": [
{
"interface_name": "intf1",
"interface_ip": "1.1.1.1",
"interface_mac": "aa : aa: aa: aa: aa"
}
]
}
现在我必须编写POJO类来绑定JSON使用JAXB注释的结构(javax.xml.bind.annotation。*)。
任何人都可以帮助我如何为复杂的JSON结构编写POJO类,将JSON转换为XML然后使用XML模式生成类是没有任何其他方法帮助吗?
提前致谢:-)
Now I have to write POJO class to bind the JSON structure using JAXB annotations (javax.xml.bind.annotation.*). Can anyone help me how to write POJO class for a complex JSON structure,converting JSON to XML and then using XML schema to generate class is not helping out is there any other way? Thanks in advance:-)
推荐答案
根据上述JSON结构,您的Java对象将如下所示:
As per the above JSON structure, your Java objects will look like this:
public class OutermostClass{
private Hardware hardware;
private String name;
private Set<Network> network = new HashSet<Network>;
}
public class Hardware {
private int cores;
private int cpu;
}
public class Network {
private String interface_name;
private String interface_ip;
private String interface_mac
}
这篇关于使用JSON数据创建POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!