Create a POJO using JSON data

拥有回忆 提交于 2020-01-15 06:49:31

问题


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"
        }
     ]
}

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:-)


回答1:


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
}


来源:https://stackoverflow.com/questions/26271526/create-a-pojo-using-json-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!