Hello! There are the following Json:

[ { "id": 1, "typeOfOrganization": "Администрации", "created_at": 1462136400000 }, { "id": 2, "typeOfOrganization": "Архив", "created_at": 1462136400000 } ] 

From the application I try to get it like this:

 return template.getForObject(Constans.URL.GET_PUBLIC_ORG_ITEM, OrgDTO.class); 

But at start exception is thrown out:

 Caused by: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of com.spravka.dto.OrgDTO out of START_ARRAY token 

However, if you write this:

 OrgDTO[] result = template.getForObject(Constans.URL.GET_PUBLIC_ORG_ITEM, OrgDTO[].class); return result[0]; 

Everything is normal, but the first element is displayed accordingly: typeOfOrganization - Administration.

What should I do to get the entire list? I suspect that this is due to the fact that json comes in an array [].

  • What should I do to get the entire list - Return not result[0] , but result ? - Nofate
  • @Nofte, underlined in red Return result. Can not convert type of expression list from java.util.List
  • Well, change the method signature as well - Nofate

1 answer 1

Obviously, your JSON is not a representation of the OrgDTO class, but a collection of objects of this class. That is why getForObject () cannot parse JSON into one object.

As Nofate rightly pointed out, you need to get it as a collection, and from there you need to get the necessary object.

 OrgDTO[] parseMyJSON() { OrgDTO[] result = template.getForObject(Constans.URL.GET_PUBLIC_ORG_ITEM, OrgDTO[].class); return result; } 

Or if the method cannot change, then transfer not all JSON to it, but only the part that represents the object you need.