如何在java中使用json data / json响应创建pojo类?

时间:2016-09-19 03:39:58

标签: java json java-ee jackson pojo

下面显示的是我的JSONdata,我想为我的JSONdata / JSONResponse创建POJO类。 如何处理使用jsonschema2pojo,我不想使用任何在线工具,我需要源代码来工作。   例如:JSONdata

 [ {
      "ADDRESS" : "ewrer23214324",
      "DESCP" : "LO-3434",
      "DEVICE size" : "1.01091E+11",
      "DIRECTORY NUMBER  1" : "\\+34343"
      } ]

1 个答案:

答案 0 :(得分:0)

首先,我在"\+34343"中有疑问,这是非法的。我考虑"+34343"。 我用杰克逊。

public class Poj implements Serializable{

    @JsonProperty("ADDRESS")
    private String address;
    @JsonProperty("DESCP")
    private String descp;
    @JsonProperty("DEVICE size")
    private Double deviceSize;
    @JsonProperty("DIRECTORY NUMBER 1")
    private Long directoryNumberOne;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getDescp() {
        return descp;
    }

    public void setDescp(String descp) {
        this.descp = descp;
    }

    public Double getDeviceSize() {
        return deviceSize;
    }

    public void setDeviceSize(Double deviceSize) {
        this.deviceSize = deviceSize;
    }

    public Long getDirectoryNumberOne() {
        return directoryNumberOne;
    }

    public void setDirectoryNumberOne(Long directoryNumberOne) {
        this.directoryNumberOne = directoryNumberOne;
    }
}



public class JsonToPojo {
    public static List<Poj>  conver(String data) throws IOException {
        ObjectMapper ob = new ObjectMapper();
       return  ob.readValue(data,new TypeReference<List<Poj>>() { });
    }

    @Test
    public void testConver() throws IOException {
        String data = "[ { \"ADDRESS\" : \"ewrer23214324\", \"DESCP\" : \"LO-3434\", \"DEVICE size\" : \"1.01091E+11\", \"DIRECTORY NUMBER 1\" : \"+34343\" } ]";

        List<Poj> list = JsonToPojo.conver(data);
        for (Poj p:list){
            System.out.println(p.getAddress()+":"+p.getDescp()+":"+p.getDeviceSize()+":"+p.getDirectoryNumberOne());
        }
    }
}