Java: Jackson String to Json - 无法反序列化 START_ARRAY 令牌中的实例
Java: Jackson String to Json - Can not deserialize instance of out of START_ARRAY token
直截了当...
我有一个方法 returns 一个字符串(见下面的示例字符串) - 本质上,我向 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);
然而,当我 运行 我的代码时,不幸的是,returns 出现以下错误...
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.my.models.MyModel out of START_ARRAY token
最终,我知道是什么导致了错误,它是对象数组,"rows"...但我不确定如何修复它。显然,我无法更改返回给我的 string/object 的架构。
任何建议将不胜感激。
更新:
我的模型
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 中进行了以下更改 class
public class MyModel {
public MyModel() {}
private int total;
private List<ModelRows> rows;
public int getTotal() { return total; }
public List<ModelRows> getRows() { return rows; }
}
直截了当...
我有一个方法 returns 一个字符串(见下面的示例字符串) - 本质上,我向 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);
然而,当我 运行 我的代码时,不幸的是,returns 出现以下错误...
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.my.models.MyModel out of START_ARRAY token
最终,我知道是什么导致了错误,它是对象数组,"rows"...但我不确定如何修复它。显然,我无法更改返回给我的 string/object 的架构。
任何建议将不胜感激。
更新: 我的模型
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 中进行了以下更改 class
public class MyModel {
public MyModel() {}
private int total;
private List<ModelRows> rows;
public int getTotal() { return total; }
public List<ModelRows> getRows() { return rows; }
}