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.

1
  • 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) Commented Feb 18, 2011 at 6:17

3 Answers 3

12
JsonNode jsonNode = mapper.readValue(s, JsonNode.class); 
JsonNode userCards = jsonNode.path("data");
List<Item> list = mapper.readValue(userCards.toString(), new TypeReference<List<Item>>(){});
Sign up to request clarification or add additional context in comments.

3 Comments

This answer would be much better if you included some text with it to explain how and why it addresses the issue.
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.
@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.
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;

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.