无法从 START_ARRAY 令牌中反序列化 `pojo.SuperRoot` 实例

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

Restassured 无法将 JSON 响应反序列化到我的 POJO 类中(这可能与 link 有关。

我的超级根班

public class SuperRoot {
    private List<Root> root; //includes getters & setters
}
public class Root { //includes getters & setters
    public List<Object> breeds;
    public String id;
    public String url;
    public int width;
    public int height;
    public List<Category> categories;
}

我的 JSON 应该被反序列化,如下所示:

[
    {
        "breeds": [
            
        ],
        "id": "1jm",
        "url": "https://cdn2.thecatapi.com/images/1jm.jpg",
        "width": 401,
        "height": 600
    },
    {
        "breeds": [
            
        ],
        "id": "75q",
        "url": "https://cdn2.thecatapi.com/images/75q.jpg",
        "width": 500,
        "height": 333
    },
    {
        "breeds": [
            
        ],
        "id": "86o",
        "url": "https://cdn2.thecatapi.com/images/86o.jpg",
        "width": 500,
        "height": 344
    }
]

反序列化应该会成功,因为根类将有一个较小的 JSON 列表

Github 项目链接 https://github.com/apoorvSinha/catProject

java rest rest-assured
1个回答
0
投票

当我使用如下所示的 TypeReference 时就解决了

List<Root> cat =new ObjectMapper().readValue(response.asString(), new TypeReference<List<Root>>(){});
        for(Root st: cat) {
            System.out.println(st.getHeight());
}
© www.soinside.com 2019 - 2024. All rights reserved.