Mirjalal
Mirjalal

Reputation: 1352

How to use Jackson to deserialise arrays of objects from json object

I am trying to deserialise the following JSON using Jackson library. This JSON is very similar which mentioned in this question. My question is: How to deserialise following JSON?

{
  "A": [
    {
      "id": 16,
      "logo": "QJQSZzbXurElfHYcq6hcbPuaWKVfQU31lx2eSIIr.png",
    },
    {
      "id": 20,
      "logo": "AizaZzbXurElfHYcq6PuaWKV2761lx2eSLESASFr.png",
    }
  ],
  "B": [
    {
      "id": 42,
      "logo": "tBYhHGTNTCYT60RZJydMyGgg47Tla36ISuRj4p0e.png",
    },
    {
      "id": 44,
      "logo": "kCZveUWo9eqIZc25deE4ln8llxlbBviRolk4PsCm.png",
    }
  ]
}

Here is MonthTree class:

public class MonthTree {
    private int id;
    private String logo;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getLogo() {
        return logo;
    }
    public void setLogo(String logo) {
        this.logo = logo;
    }
}

However, I tried to get list/array of array names (the A and B), id and logo properties, but I failed with it. Here is what I tried to do at all:

ObjectMapper mapper = new ObjectMapper();
List<MonthTree> monthTrees = mapper.readValue(json_res, new TypeReference<List<MonthTree>>(){});

So, I got the following exception:

Cannot deserialize instance of com.talmir.myApp.utils.MonthTree[] out of START_OBJECT token

p.s. I am new with this library, so don't know what else functionalities this library has.

Upvotes: 0

Views: 48

Answers (1)

Bruno Carletti
Bruno Carletti

Reputation: 273

You can do by this way:

public class MonthTree {

    private int id;
    private String logo;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getLogo() {
        return logo;
    }
    public void setLogo(String logo) {
        this.logo = logo;
    }
    @Override
    public String toString() {
        return "MonthTree [id=" + id + ", logo=" + logo + "]";
    }

    public static void main(String[] args) {
        String json = "{\n" + 
                "  \"A\": [\n" + 
                "    {\n" + 
                "      \"id\": 16,\n" + 
                "      \"logo\": \"QJQSZzbXurElfHYcq6hcbPuaWKVfQU31lx2eSIIr.png\"\n" + 
                "    },\n" + 
                "    {\n" + 
                "      \"id\": 20,\n" + 
                "      \"logo\": \"AizaZzbXurElfHYcq6PuaWKV2761lx2eSLESASFr.png\"\n" + 
                "    }\n" + 
                "  ],\n" + 
                "  \"B\": [\n" + 
                "    {\n" + 
                "      \"id\": 42,\n" + 
                "      \"logo\": \"tBYhHGTNTCYT60RZJydMyGgg47Tla36ISuRj4p0e.png\"\n" + 
                "    },\n" + 
                "    {\n" + 
                "      \"id\": 44,\n" + 
                "      \"logo\": \"kCZveUWo9eqIZc25deE4ln8llxlbBviRolk4PsCm.png\"\n" + 
                "    }\n" + 
                "  ]\n" + 
                "}";

        try {
            ObjectMapper mapper = new ObjectMapper();
            Map<String, List<MonthTree>> map = mapper.readValue(json, new TypeReference<Map<String, List<MonthTree>>>(){});
            Iterator<String> it = map.keySet().iterator();
            while (it.hasNext()) {
                String letter = (String) it.next();
                List<MonthTree> list = map.get(letter);
                for (MonthTree monthTree : list) {
                    System.out.println("Letter: "+letter+" MonthTree: "+monthTree);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Upvotes: 1

Related Questions