6

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")?

bruno
  • 2,131
  • 1
  • 17
  • 30
localhost
  • 5,500
  • 1
  • 32
  • 51
  • if you are getting this JSON from any rest endpoint then try spring rest template, it has messgae converters which will take care of marshalling and unmarshalling – ppuskar Dec 30 '14 at 04:45

1 Answers1

4

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);
Sotirios Delimanolis
  • 263,859
  • 56
  • 671
  • 702