2016-09-19 51 views
0

下面顯示的是我的JSON數據,我想爲我的JSON數據/ JSONResponse創建POJO類。 如何處理使用jsonschema2pojo,我不想使用任何在線工具,我需要源代碼工作。 例如:JSONdata如何在java中使用json數據/ json響應創建pojo類?

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

的〔使用JSON數據創建POJO](http://stackoverflow.com/questions/26271526/create-a-pojo-using-json-data)生成Java類的 – emotionlessbananas

+0

可能的複製可能的複製從JSON?](http://stackoverflow.com/questions/1957406/generate-java-class-from-json) –

回答

0

首先,我有問題的"\+34343",它是illegal.I考慮"+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()); 
     } 
    } 
} 
+0

感謝荒謬,更多的問題我的JSON數據不固定它將繼續改變不同的屬性,所以我需要維護一切pojo類中的任何時候,當我得到新的jsonresponse是否有可能做到這一點? – sharan