如何使用Jackson从json对象反序列化对象数组

问题描述 投票:0回答:1

我试图使用杰克逊库反序列化以下JSON。这个JSON非常类似于this question中提到的。我的问题是:如何根据JSON进行反序列化?

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

这是MonthTree类:

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;
    }
}

但是,我试图得到列表/数组的数组名称(AB),idlogo属性,但我失败了。这是我试图做的事情:

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

所以,我得到以下异常:

无法从START_OBJECT标记中反序列化com.talmir.myApp.utils.MonthTree[]的实例

附:我是这个库的新手,所以不知道这个库有什么功能。

android json jackson
1个回答
1
投票

你可以这样做:

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();
        }

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.