android jackson json object mapper array deserialization

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.

  • Changing number of columns with GridLayoutManager and RecyclerView
  • How does Android handle background threads when leaving an Activity?
  • Android System Color Constants
  • Set OnClick Listener on Action Bar Title in Android
  • How to get the number of items in a spinner?
  • OnClickListener of Child preventing the swipe of Android ViewPager
  • 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.

    Related posts:

    Android material design buttons - Pre lollipop
    What is default color for text in textview?
    How to prevent ActivityUnitTestCase from calling Application.onCreate?
    adb command not found
    HelloAndroid emulator problem
    How to implement Google Maps new version of API v2
  • Why use newInstance for DialogFragment instead of the constructor?
  • How to prevent Android's drawBitmap from only drawing black images?
  • Android Error - Open Failed ENOENT
  • Package rename and error “Activity class does not exist”
  • Why PLAY_SERVICES_RESOLUTION_REQUEST is 9000
  • Android: StaggeredGridLayoutManager scroll to top while initializing
  • 3 Solutions collect form web for “android jackson json object mapper array deserialization”

    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;
    
    JsonNode jsonNode = mapper.readValue(s, JsonNode.class); 
    JsonNode userCards = jsonNode.path("data");
    List<Item> list = mapper.readValue(userCards.toString(), new TypeReference<List<Item>>(){});
    

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.