android jackson json object mapper array deserialization

DArkO picture DArkO · Feb 17, 2011 · Viewed 9.5k times · Source

i need help parsing a response with the jackson mapper to a POJO. i have this as a response:

 "data": [{
        "item": {
            "downloaded": false,
            "moderated": false,
            "add": false
        }
    },
    {
        "item": {
            "downloaded": false,
            "moderated": false,
            "add": false }
// more

so how do i bind this one with the mapper to a POJO? here is my class that i am trying but it returns that "item" is not recognized and not allowed to be ignored.

public ArrayList<Item> data =  new ArrayList<Item>();

where item is a public static class Item with constructors and all the fields above with getters and setters.

how do i do this. i cant seem to find anywhere how to read data from an array this way.

Answer

maziar picture maziar · Oct 16, 2011
JsonNode jsonNode = mapper.readValue(s, JsonNode.class); 
JsonNode userCards = jsonNode.path("data");
List<Item> list = mapper.readValue(userCards.toString(), new TypeReference<List<Item>>(){});