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

时间:2018-09-01 15:07:57

标签: android json jackson

我正在尝试使用Jackson库反序列化以下JSON。该JSON与this question中提到的JSON非常相似。我的问题是: 如何根据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[]的实例

p.s。我是这个库的新手,所以不知道这个库还有什么其他功能。

1 个答案:

答案 0 :(得分: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();
        }

    }

}