How to parse a JSON string to an array using Jackson


I have a String with the following value:

    "key1": "value11",
    "key2": "value12"
    "key1": "value21",
    "key2": "value22"

And the following class:

public class SomeClass {
    private String key1;
    private String key2;
    /* ... getters and setters omitted ...*/

And I want to parse it to a List<SomeClass> or a SomeClass[]

Which is the simplest way to do it using Jackson ObjectMapper?

This question is tagged with java json jackson

~ Asked on 2011-08-30 16:01:03

The Best Answer is


I finally got it:

ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
List<SomeClass> someClassList = objectMapper.readValue(jsonString, typeFactory.constructCollectionType(List.class, SomeClass.class));

~ Answered on 2011-08-30 16:26:13


The other answer is correct, but for completeness, here are other ways:

List<SomeClass> list = mapper.readValue(jsonString, new TypeReference<List<SomeClass>>() { });
SomeClass[] array = mapper.readValue(jsonString, SomeClass[].class);

~ Answered on 2011-08-31 11:55:28

Most Viewed Questions: