0

the json like this

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

How could I decode it to java class?

I write a class like

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;
    }

}

and read json

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

the error is

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

at [Source: java.io.StringReader@4150c480; line: 1, column: 81] (through reference chain: com.rayboot.wl.object.UploadedInfoObjEx["infoItems"])

anyone could help me? thanks

1 Answer 1

2

I think you need to make the variables in your Items class public. You may also need to make it static:

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;
}

If that doesn't work you could try moving Items to its own file.

Sign up to request clarification or add additional context in comments.

1 Comment

Either make them public; add @JsonProperty annotation; or change default introspection visibility to "see" private fields too. Many ways to achieve this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.