11

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.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
DArkO
  • 15,880
  • 12
  • 60
  • 88
  • JSON structure looks quite redundant: why are there 'item' entries there? Is this converted from XML or something? Also: please include definition of Item class (or indicate that you'd like it to be defined too) – StaxMan Feb 18 '11 at 06:17

3 Answers3

12
JsonNode jsonNode = mapper.readValue(s, JsonNode.class); 
JsonNode userCards = jsonNode.path("data");
List<Item> list = mapper.readValue(userCards.toString(), new TypeReference<List<Item>>(){});
The Student Soul
  • 2,272
  • 2
  • 14
  • 12
maziar
  • 581
  • 3
  • 9
  • This answer would be much better if you included some text with it to explain *how* and *why* it addresses the issue. – Flexo Oct 16 '11 at 15:46
  • I found this approach preferable to that by StaxMan. Reason being is that StaxMan's requires an extra object to wrap the array/list. This approach only requires an extra line. I rewrote my code slightly differently from maziar's, eliminates an extra line. See my answer, though it's just a variant of maziar's. – Bane Mar 23 '12 at 07:36
  • @awoodland : someone can probably explain better than me; but, in simple terms, the problem is that the OP's JSON isn't a single object of type Item, it's an array/list of Items. You've got to tell Jackson to expect a list, not an object. See my answer below, which is simpler, and it might make more sense to you. Converting to a JsonNode first works, but seems superfluous (AFAIK). I could be wrong though, maybe something is missing in the output. Seems to work for me though. – Bane Mar 23 '12 at 07:44
8

Your example is missing couple of pieces (esp. definition of Item), to know if your structure is compatible; but in general JSON and Object structures need to match. So, you would at least need something like:

public class DataWrapper {
  public List<Item> data; // or if you prefer, setters+getters
}

and if so, you would bind with:

DataWrapper wrapper = mapper.readValue(json, DataWrapper.class);

and access data as

List<Item> items = wrapper.data;
StaxMan
  • 113,358
  • 34
  • 211
  • 239
2

Here's my variant of maziar's code.

List<Item> list = mapper.readValue( s, new TypeReference<List<Item>>(){} );

It just eliminates the converting first to a JsonNode and converts instead directly to a List; still works fine, outputting a list of Items.

Bane
  • 1,772
  • 3
  • 21
  • 28