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,213
  • 1
  • 19
  • 31
localhost
  • 5,568
  • 1
  • 33
  • 53
  • 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
  • 274,122
  • 60
  • 696
  • 724