简体   繁体   中英

Jackson - How to parse this field?

I have the following JSON (coming from a WebService):

{
    "result": "success",
    "users": [{
        "id": 12345,
        "login": "blabla",
        "firstName": "first name here",
        "lastName": "last name here",
        "companyName": "company here",
        "email": "email_here@test.com",
        "phone": "",
        "mobile": "123456789",
        "locations": [{
            "id": 123123,
            "latitude": 23.330196,
            "longitude": -92.026073,
            "timestamp": "2015-08-17T01:43:21+00:00"
        }],
        "status": {
            "message": "Message here",
            "timestamp": "2015-07-31T01:50:51+00:00"
        }
    }, (...)

and I also have the following bean:

@JsonTypeName("user")
public class User implements IdentityInterface
{
    private long id;

    @JsonProperty
    private String firstName;

    (...)

    //How can I anotate this field?
    private double latitude;

    //How can I anotate this field?
    private double longitude;

    (...)
}

I can't just put @JsonProperty in the latitude/longitude variables because those values are inside the "locations" part of the JSON.

How can I annotate the latitude / longitude fields with Jackson here?

You need to create a List of locations in your User class like so.

@JsonTypeName("user")
public class User implements IdentityInterface
{
    private long id;

    @JsonProperty
    private String firstName;

    (...)

    @JsonProperty
    private List<Location> locations;
}
public class Location{
    private Long id;
    private Double latitude;
    private Double longitude;
    private Date timestamp;

    // Getters and setter
   (...)
}

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