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...
{ "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
public class MyModel { public MyModel() {} private int total; private ModelRows rows; public int getTotal() { return total; } public ModelRows getRows() { return rows; } } UPDATE: 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; } }
MyModelrowsneed to be a collection in your model