gpt4 book ai didi

Java:Jackson 字符串到 Json - 无法反序列化 START_ARRAY token 之外的实例

转载 作者:行者123 更新时间:2023-12-02 13:22:02 30 4
gpt4 key购买 nike

直接开始...

我有一个返回字符串的方法(请参阅下面的示例字符串) - 本质上,我向 URL 发出 HTTP GET 请求,响应是下面的字符串...

{
"total": 30,
"rows": [
{
"id": 1,
"parent": "parentA",
"children": "childB, childC, childD"
},
{
"id": 2,
"parent": "parentE",
"children": "childF, childG, childH"
},
{
"id": 3,
"parent": "parentI",
"children": "childJ, childK, childL"
},
{
"id": 4,
"parent": "parentM",
"children": "childN, childO"
},
{
"id": 5,
"parent": "parentP",
"children": "childQ, childR, childS, childT"
},
{
"id": 6,
"parent": "parentU",
"children": "childV, childW, childX"
},
{
"id": 7,
"parent": "parentY",
"children": "childZ"
}
]
}

然后我将此字符串分配给一个变量,然后将其映射到我的模型......

String strRel = <JSON OBJECT FROM ABOVE>
ObjectMapper mapper = new ObjectMapper();
MyModel obj = mapper.readValue(strRel, MyModel.class);

但是,当我运行代码时,不幸的是,它返回以下错误......

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.my.models.MyModel out of START_ARRAY token

最终,我知道是什么导致了错误,它是对象数组,“行”......但我不知道如何修复它。显然,我无法更改返回给我的字符串/对象的架构。

任何建议将不胜感激。

更新:我的模型

public class MyModel {
public MyModel() {}

private int total;
private ModelRows rows;

public int getTotal() { return total; }
public ModelRows getRows() { return rows; }
}

更新:模型行

public class ModelRows {
public ModelRows() {}

private int id;
private String parent;
private String children;

public int getId() { return id; }

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

public String getParent() { return parent; }

public void setParent(String parent) { this.parent = parent; }

public String getChildren() { return children; }

public void setChildren(String children) { this.children = children; }
}

最佳答案

在 MyModel 类中进行以下更改

public class MyModel {
public MyModel() {}

private int total;
private List<ModelRows> rows;

public int getTotal() { return total; }
public List<ModelRows> getRows() { return rows; }
}

关于Java:Jackson 字符串到 Json - 无法反序列化 START_ARRAY token 之外的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43527522/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com