[英]Java: Jackson String to Json - Can not deserialize instance of out of START_ARRAY token
Getting straight to it... 直奔它...
I have a method that returns a string (see example string below) - essentially, I make a HTTP GET Request to a URL and the response is the string below... 我有一个返回字符串的方法(请参见下面的示例字符串)-本质上,我向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"
}
]
}
I then assign this string to a variable, then map it to my model... 然后,我将此字符串分配给变量,然后将其映射到我的模型...
String strRel = <JSON OBJECT FROM ABOVE>
ObjectMapper mapper = new ObjectMapper();
MyModel obj = mapper.readValue(strRel, MyModel.class);
However, when I run my code, it, unfortunately, returns the following error... 但是,不幸的是,当我运行代码时,它返回以下错误...
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.my.models.MyModel out of START_ARRAY token
Ultimately, I know what is causing the error, it's the Array of Objects, "rows"...but I am not sure how to fix it. 最终,我知道是什么导致了错误,这是对象数组,“行” ...但是我不确定如何解决它。 Obviously, I cannot change the schema of the string/object coming back to me.
显然,我无法更改返回给我的字符串/对象的架构。
Any advice will be greatly appreciated. 任何建议将不胜感激。
UPDATE: MyModel 更新: MyModel
public class MyModel {
public MyModel() {}
private int total;
private ModelRows rows;
public int getTotal() { return total; }
public ModelRows getRows() { return rows; }
}
UPDATE: ModelRows 更新: ModelRows
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; }
}
Made below changes in your MyModel class 在MyModel类中进行以下更改
public class MyModel {
public MyModel() {}
private int total;
private List<ModelRows> rows;
public int getTotal() { return total; }
public List<ModelRows> getRows() { return rows; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.