Cannot Deserialize Instance of Object Out of Start_Array Token in Spring Webservice

Cannot deserialize instance of object out of START_ARRAY token in Spring Webservice

Your json contains an array, but you're trying to parse it as an object.
This error occurs because objects must start with {.

You have 2 options:

  1. You can get rid of the ShopContainer class and use Shop[] instead

    ShopContainer response  = restTemplate.getForObject(
    url, ShopContainer.class);

    replace with

    Shop[] response  = restTemplate.getForObject(url, Shop[].class);

    and then make your desired object from it.

  2. You can change your server to return an object instead of a list

    return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(list);

    replace with

    return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(
    new ShopContainer(list));

Cannot deserialize instance of object out of START_ARRAY token in Spring 3 REST Webservice

Solution in a similar situation was to map to Consultant[].class
This applies if you try to deserialize a JSON array of your mapped objects.

Spring: Cannot deserialize instance of ENTITY out of START_OBJECT token

As you can see on the JSON content that you provided, a response from your method is not a list or array. It is an object.

ResponseEntity<User[]> response = restTemplate.getForEntity(
"/users",
User[].class
);

This will not work. You have to create a new class like this:

class Embedded {
private List<User> users;
}
class Response {
private Embedded _embedded;
}

ResponseEntity<User[]> response = restTemplate.getForEntity(
"/users",
Response.class
);

Issue with parsing the content from JSON file with Jackson & message- JsonMappingException -Cannot deserialize as out of START_ARRAY token

Your JSON string is malformed: the type of center is an array of invalid objects. Replace [ and ] with { and } in the JSON string around longitude and latitude so they will be objects:

[
{
"name" : "New York",
"number" : "732921",
"center" : {
"latitude" : 38.895111,
"longitude" : -77.036667
}
},
{
"name" : "San Francisco",
"number" : "298732",
"center" : {
"latitude" : 37.783333,
"longitude" : -122.416667
}
}
]

Cannot deserialize instance of `model.UsersPajo` out of START_ARRAY token at [Source: (String)[UsersPajo{website='hildegard.org'

The issue is the following lines of code:

try {
usersPajo = objectMapper.readValue(information, UsersPajo.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}

The variable information contains the following:

[UsersPajo{website='hildegard.org', address=Address{zipcode='92998- 
3874', geo=Geo{lng='81.1496', lat='-37.3159'}, suite='Apt. 556', city='Gwenborough',
street='Kulas Light'}, phone='1-770-736-8031 x56442', name='Leanne Graham',
company=Company{bs='harness real-time e-markets', catchPhrase='Multi-layered client-server
neural-net', name='Romaguera-Crona'}, id=1, email='Sincere@april.biz', username='Bret'},
UsersPajo{website='anastasia.net', address=Address{zipcode='90566-7771',
geo=Geo{lng='-34.46"....

Which basically is the usersPajos2 mapped to a String. This obviously can't be deserialized into a single UsersPajo instance. So get rid of these lines of code and keep all others:

public static String readUserFile(String titleFile) {
String pathJSONFile = "src/main/resources/" + titleFile + ".json";
ObjectMapper objectMapper = new ObjectMapper();
File jsonFile = new File(pathJSONFile);
UsersPajo[] usersPajos2= null;

try {
usersPajos2 = objectMapper.readValue(jsonFile, UsersPajo[].class);
System.out.println("_usersPajos2___" + usersPajos2);
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return Arrays.toString(usersPajos2);
}

object out of START_ARRAY token in Spring resttemplate

I got it weeks ago, and I feel that I should post my solution here for future use.

My JSON Response structure is a list of key value pairs, so what I did is

List<HashMap<String,String>> map=restTemplate.porsForObject(url,mvm,List.class);

The JSON response was converted into a map. So it is easy now to access the data by doing this.

String data= (Hashmap) map.get(position).get(key);


Related Topics



Leave a reply



Submit