0

像这样的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?)

在 [来源:java.io.StringReader@4150c480;行:1,列:81](通过引用链:com.rayboot.wl.object.UploadedInfoObjEx["infoItems"])

任何人都可以帮助我吗?谢谢

4

1 回答 1

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到它自己的文件。

于 2012-11-08T05:10:31.483 回答