Parsing a List with Jackson

Posted on 15 Mar 2013 by Eric Oestrich

I started on a new Android project that pulls JSON form a web service. One of the end points returns and array of objects and I wanted to parse them into Java objects with Jackson. It wasn’t very obvious and I didn’t see this listed anywhere so I figured I’d share it.

Replace MyClass with the class you want to deserialize to.

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

TypeFactory typeFactory = TypeFactory.defaultInstance();
List<MyClass> my_classes = mapper.readValue(responseJson, typeFactory
    .constructCollectionType(ArrayList.class, MyClass.class));

comments powered by Disqus
Creative Commons License
This site's content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License unless otherwise specified. Code on this site is licensed under the MIT License unless otherwise specified.