0

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; } } 
5
  • Post the code of MyModel Commented Apr 20, 2017 at 18:48
  • 3
    The rows need to be a collection in your model Commented Apr 20, 2017 at 18:50
  • @Mubin...there you go Commented Apr 20, 2017 at 18:56
  • @Pete, can you post your comment as an answer so I can give you credit... Commented Apr 20, 2017 at 19:08
  • @DidierJeanCharles no need! glad that the comment helped you to solve the problem. Commented Apr 24, 2017 at 2:55

1 Answer 1

1

Made below changes in your 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; } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.