2012-11-08 111 views
0

的JSON這樣JSON - 傑克遜陣列分析

{ 
     "resultCode": "0000", 
     "resultMsg": "", 
     "pageCount": "6", 
     "curPage": "1", 
     "infoItems": [ 
     { 
      "sID": "268", 
      "location": "222", 
      "unit": "", 
      "time": "2012-11-02 17:51:46", 
      "longitude": "111", 
      "latitude": "222", 
      "reason": "some", 
      "dealContent": "" 
     }, 
     { 
      "sID": "267", 
      "location": "fgg", 
      "unit": "yyg", 
      "time": "2012-11-02 17:49:14", 
      "longitude": "111", 
      "latitude": "222", 
      "reason": "some", 
      "dealContent": "" 
     } 
    ] 
} 

我怎麼能譯碼後,java類?

我寫像

public class UploadedInfoObjEx { 
    public String resultCode; 
    public String resultMsg; 
    public String pageCount; 
    public String curPage; 
    public Items[] infoItems; 

    public class Items { 
     private String sID; 
     private String location; 
     private String unit; 
     private String longitude; 
     private String latitude; 
     private String reason; 
     private String time; 
     private String dealContent; 
    } 

} 

類和讀JSON

  ObjectMapper mapper = new ObjectMapper(); 
      UploadedInfoObjEx uploadedInfoObjEx = mapper.readValue(jsonString, UploadedInfoObjEx.class); 

誤差

org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.rayboot.wl.object.UploadedInfoObjEx$Items]: can not instantiate from JSON object (need to add/enable type information?) 

在[來源:[email protected]; line:1,column:81](通過參考鏈:com.rayboot.wl.object.UploadedInfoObjEx [「infoItems」])

任何人都可以幫助我? 謝謝

回答

2

我認爲你需要使你的Items類變量公開。您可能還需要使其靜態:

public static class Items { 
    public String sID; 
    public String location; 
    public String unit; 
    public String longitude; 
    public String latitude; 
    public String reason; 
    public String time; 
    public String dealContent; 
} 

如果不工作,你可以嘗試移動Items到自己的文件。

+0

要麼讓他們公開;添加'@ JsonProperty'註釋;或者將默認的自省可見性更改爲「查看」私人領域。許多方法來實現這一點。 – StaxMan