Jackson Nested Mapping

I get JSONString as the answer as below

 {"size":3,"page-size":3,"subscribers-aircraft":
[
{"company-name":"ATCOMPANYTEST","tailno":"N345","status":"ACTIVE","network-status":"ACTIVE","id":18501,"contact":"SS Theairtime","model":"A-380-800","note":null,"created-at":"Jun 29, 2015 7:42:54 AM","packages":2,"username":"ATAIRCRAFTTEST","external-account":null,"consumer-type":"AIRCRAFT","isp-id":18493,"parent-id":18497,"subscriber-feature-id":13,"status-code":null,"status-msg":null},
{"company-name":"The AIRTIME 3","tailno":"VT456","status":"ACTIVE","network-status":"ACTIVE","id":18489,"contact":"Ramesh Anantarapu","model":"C-21","note":null,"created-at":"Jun 25, 2015 8:27:16 AM","packages":0,"username":"ramesh23","external-account":null,"consumer-type":"AIRCRAFT","isp-id":18469,"parent-id":18473,"subscriber-feature-id":9,"status-code":null,"status-msg":null},
{"company-name":"The AIRTIME 3","tailno":"VT23","status":"ACTIVE","network-status":"OFFLINE","id":18485,"contact":"Ramesh Anantarapu","model":"747-100","note":null,"created-at":"Jun 23, 2015 2:49:15 PM","packages":1,"username":"ramesh","external-account":null,"consumer-type":"AIRCRAFT","isp-id":18469,"parent-id":18473,"subscriber-feature-id":5,"status-code":null,"status-msg":null}],
"status-code":"AIRTIME-P4000","status-msg":"Successfully"}

This is a flat answer.

Now I'm trying to map it to the upper level POJO, called AircraftInfoone that contains the other two POJOsas its objects.

While, for example, the company name, tailNo and status belong directly to AircraftInfo during the username, the identifier belongs to UserInfo POJO, which is an entity in AircraftInfo.

Since the JSON response is flat, it is almost impossible to display such a response in a nested structure.

Can anyone suggest the most elegant way to achieve?

+4
source share
2 answers

@JsonCreator :

public static class AircraftInfo {

    private final String companyName;

    private final String tailNo;

    private final UserInfo userInfo;

    @JsonCreator
    public AircraftInfo(Map<String, Object> flatProperties) {
        this.companyName = (String) flatProperties.get("company-name");
        this.tailNo = (String) flatProperties.get("tailno");
        UserInfo user = new UserInfo();
        user.setUserName((String) flatProperties.get("username"));
        user.setId((Integer) flatProperties.get("id"));
        this.userInfo = user;
    }

    // getters for final fields
}

UserInfo :

public class UserInfo {

    private String userName;

    private int id;

    // getters and setters
}

JSON Response POJO:

public class Response {

    private int size;

    @JsonProperty("page-size") // non standard JSON property name
    private int pageSize;

    @JsonProperty("subscribers-aircraft") // non standard JSON property name
    private List<AircraftInfo> subscribersAircraft; // use class defined above!

    @JsonProperty("status-code") // non standard JSON property name
    private String statusCode;

    @JsonProperty("status-msg") // non standard JSON property name
    private String statusMsg;

    // getters and setters
}

2 :

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setSerializationInclusion(Include.NON_NULL);
String json = // your exact json here, ESCAPED!
Response response = mapper.readValue(json, Response.class);

Response , JSON.

+2

Deserializer. , API- Jackson Streaming - .

, : JSON HashMap ( ObjectMapper.readValue() Map.class second arg),

+1

Source: https://habr.com/ru/post/1606755/


All Articles