简体   繁体   English

使用Jackson反序列化JSON的问题

[英]Issue with deserializing JSON using Jackson

I have such JSON 我有这样的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. 我想将它反序列化为Java类,但我只对一些JSON字段感兴趣。 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; org.codehaus.jackson.map.exc.UnrecognizedPropertyException:无法识别的字段“body”(类com.demo.Response),未标记为可忽略[来源:java.io.StringReader@6483f5ae; line: 1, column: 10] (through reference chain: com.demo.Response["body"]) line:1,column:10](通过引用链:com.demo.Response [“body”])

Here is code(the JSON string is correctly received and has same format as I initially mentioned in the beginning): 这是代码(正确接收JSON字符串并且格式与我最初在开头提到的格式相同):

 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. 您需要为字段创建getter和setter,并且应该为字段添加注释。

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/ . 您可以使用http://www.jsonschema2pojo.org/自动从json创建您的pojos。 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: 正如其他人所提到的,默认情况下private字段不会自动检测,因此:

  • Annotating fields with @JsonProperty OR 使用@JsonProperty OR注释字段
  • Adding setter 添加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. 但是,还有另一种可能性:您可以使用注释@JsonAutoDetect来更改所需的最低可见性,并在此处启用所有字段的发现。 Or you can even change the defaults used via ObjectMapper method (something like setVisibility(...) ). 或者您甚至可以更改通过ObjectMapper方法使用的默认值(类似于setVisibility(...) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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