【发布时间】:2017-04-20 18:46:12
【问题描述】:
直奔主题...
我有一个返回字符串的方法(请参阅下面的示例字符串)-本质上,我向 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的代码 -
rows必须是模型中的集合 -
@Mubin...你去吧
-
@Pete,您能否发表您的评论作为答案,以便我给您信用...
-
@DidierJeanCharles 不需要!很高兴评论帮助您解决了问题。