简体   繁体   中英

Issue with deserializing JSON using Jackson

I have such JSON

{"body":{"result":[{"crossStateId":1,"raceId":181564,"withOfficer":1,"documents":[{"indexed":0,"documentNumber":"zzz","isMain":1,"documentTypeId":6,"serverId":16,"countryId":327,"useDate":"2017-02-07T19:31:51.000+0000","documentSubTypeId":6,"crossId":5018177,"documentId":44973231,"personId":222,"infinity":0,"documentValid":"2023-08-25T20:00:00.000+0000"}],"directionId":2,"documentNumber":"sss","operatorUsername":"AIRPORT_84","crossDate":"2017-02-07T19:31:51.000+0000","serverId":16,"crossTypeId":1,"crossRegisterDate":"2017-02-07T19:31:52.818+0000","officerNote":"","children":[],"personNote":"","crossId":5018177,"workplaceId":82,"divisionId":2,"race":{"carriageContainer":0,"raceId":181564,"raceStateId":1,"directionId":2,"creatorId":415,"countryId":327,"transportIdByType":605,"raceDateTime":"2017-02-07T19:20:58.000+0000","raceNumber":"841 sss sss","creatorUsername":"AIRPORT_8","divisionId":2,"transportTypeId":3,"createDate":"2017-02-07T19:20:58.000+0000"},"syncState":0,"autos":[],"userId":491,"raceNumber":"841 sss sss","operatorNote":"","person":{"firstNameEn":"JUMBERI","indexed":1,"lastNameGe":"ჩოხელი","genderId":2,"personId":6027803,"personalNumber":"222","countryNameGe":"sss","birthDate":"1963-06-14T20:00:00.000+0000","lastNameEn":"sss","countryId":327,"firstNameGe":"sss"},"airplane":{"raceNumber":"841 sss sss","airCompanyId":1,"airplaneId":605,"airportId":5657,"bortNumber":"01","transportSubTypeId":78,"countryId":360},"underAge":0,"personId":6027803,"decisionId":22}],"total":8264},"errorCode":0}

I would like to deserialize it to Java class but I am interested in only some JSON fields. Anyway here are the model classes:

public class Response implements Serializable {
    private Body body;
    private long errorCode;

}

public class Body implements Serializable {
    Result result[];
}

public class Result implements Serializable {

    private long crossStateId;
    private long raceId;

    private Person person;
    private Child children [];
    private Auto autos[];

}

etc.

But for some reason I get following exception:

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "body" (Class com.demo.Response), not marked as ignorable at [Source: java.io.StringReader@6483f5ae; line: 1, column: 10] (through reference chain: com.demo.Response["body"])

Here is code(the JSON string is correctly received and has same format as I initially mentioned in the beginning):

 String res = MainProgram.sendGet("someURL");

 ObjectMapper objectMapper = new ObjectMapper();
 Response ob = objectMapper.readValue(res, Response.class);

I would appreciate some help.

You need to create getters and setters for the fields, and you should add annotations to your fields.

Annotation:

@JsonProperty(value = "body")
private Body body;

Doing one of above will make it work.

Sidenote:

You can create your pojos from json automatically with http://www.jsonschema2pojo.org/ . Just paste it in and download it, or use one of their plugins.

As mentioned by others, private fields are not auto-detect by default, so either:

  • Annotating fields with @JsonProperty OR
  • Adding setter

is needed for deserialization.

However, there is another possibility: you can use annotations @JsonAutoDetect to change minimum visibility needed, and here enable discovery of ALL fields. Or you can even change the defaults used via ObjectMapper method (something like setVisibility(...) ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM