Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize wrapped list using Jackson

I have a JSON Object like this:

{"geonames":[
   {"countryId":"2017370",
    "adminCode1":"73"},
   {"countryId":"2027370",
    "adminCode1":"71"},
    ...]}

How can i deserialize this object DIRECTLY to List<GeoName>, ignoring the first layer (geonames wrapper), instead of deserializing to a wrapper object containing List<GeoName> as @JsonProperty("geonames")?

like image 350
localhost Avatar asked Dec 30 '14 04:12

localhost


1 Answers

Use an ObjectReader with a root name

ObjectMapper mapper = new ObjectMapper();
ObjectReader reader = mapper.reader(new TypeReference<List<GeoName>>() {}).withRootName("geonames");
List<GeoName> list = reader.readValue(json);
like image 98
Sotirios Delimanolis Avatar answered Oct 20 '22 10:10

Sotirios Delimanolis