Could not read JSON: Can not deserialize instance of hello.Country[] out of START_OBJECT token

Volodymyr Levytskyi

I have rest url that gives me all countries - http://api.geonames.org/countryInfoJSON?username=volodiaL.

I use RestTemplate from spring 3 to parse returned json into java objects:

RestTemplate restTemplate = new RestTemplate();
Country[] countries = restTemplate.getForObject("http://api.geonames.org/countryInfoJSON?username=volodiaL",Country[].class);

When I run this code I get an exception:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of hello.Country[] out of START_OBJECT token
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1846149; line: 1, column: 1]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:691)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:685)
    at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.handleNonArray(ObjectArrayDeserializer.java:222)
    at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:133)
    at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:18)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2158)
    at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:225)
    ... 7 more

Finally my Country class:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Country {
    private String countryName;
    private long geonameId;

    public String getCountryName() {
        return countryName;
    }

    public long getGeonameId() {
        return geonameId;
    }

    @Override
    public String toString() {
        return countryName;
    }
}

The problem is that returned json contains root element "geonames" which contains array of country elements like so:

{
"geonames": [
    {
        "continent": "EU",
        "capital": "Andorra la Vella",
        "languages": "ca",
        "geonameId": 3041565,
        "south": 42.42849259876837,
        "isoAlpha3": "AND",
        "north": 42.65604389629997,
        "fipsCode": "AN",
        "population": "84000",
        "east": 1.7865427778319827,
        "isoNumeric": "020",
        "areaInSqKm": "468.0",
        "countryCode": "AD",
        "west": 1.4071867141112762,
        "countryName": "Andorra",
        "continentName": "Europe",
        "currencyCode": "EUR"
    }
]
}

How to tell RestTemplate to convert each element of array into Country object?

geoand

You need to do the following:

public class CountryInfoResponse {

   @JsonProperty("geonames")
   private List<Country> countries; 

   //getter - setter
}

RestTemplate restTemplate = new RestTemplate();
List<Country> countries = restTemplate.getForObject("http://api.geonames.org/countryInfoJSON?username=volodiaL",CountryInfoResponse.class).getCountries();

It would be great if you could use some kind of annotation to allow you to skip levels, but it's not yet possible (see this and this)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JSON deserialization throwing exception - Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

From Java

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

From Dev

Can not deserialize instance of my.package.name.PlaceData[] out of START_OBJECT token

From Dev

JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token

From Dev

Exception: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at

From Dev

JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token

From Dev

JsonMappingException: Can not deserialize instance of java.util.List out of START_OBJECT token

From Dev

Exception: "org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token"

From Dev

jsonMappingException org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at

From Dev

Convert JSON to Object throws JsonMappingException "Can not deserialize instance of class out of START_ARRAY token"

From Dev

Can not deserialize instance of Object out of START_ARRAY token

From Dev

Can not deserialize instance of Object out of START_ARRAY token

From Dev

AWS Can not deserialize instance of java.lang.String out of START_OBJECT

From Dev

Json Mapping Exception can not deserialize instance out of START_ARRAY token

From Dev

Can not deserialize instance of JsonGen out of START_ARRAY token : newbee

From Dev

Can not deserialize instance of my package out of START_ARRAY token

From Dev

Can not deserialize instance of SaleListDTO out of START_ARRAY token

From Dev

Java REST API: Can not deserialize instance of Object out of START_ARRAY token

From Dev

com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token

From Dev

Java REST API: Can not deserialize instance of Object out of START_ARRAY token

From Dev

Can not deserialize instance of java.util.HashMap out of START_ARRAY token

From Dev

Ignore the "Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token" error

From Java

com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token - JAVA

From Dev

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.springframework.data.domain.Sort out of START_ARRAY token

From Dev

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

From Dev

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of Object out of START_ARRAY token

From Dev

ObjectMapper fails to deserialize - cannot deserialize instance of .... out of START_ARRAY token

From Dev

Jackson Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token

From Java

Jackson error: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token

Related Related

  1. 1

    JSON deserialization throwing exception - Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

  2. 2

    Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

  3. 3

    Can not deserialize instance of my.package.name.PlaceData[] out of START_OBJECT token

  4. 4

    JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token

  5. 5

    Exception: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at

  6. 6

    JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token

  7. 7

    JsonMappingException: Can not deserialize instance of java.util.List out of START_OBJECT token

  8. 8

    Exception: "org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token"

  9. 9

    jsonMappingException org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at

  10. 10

    Convert JSON to Object throws JsonMappingException "Can not deserialize instance of class out of START_ARRAY token"

  11. 11

    Can not deserialize instance of Object out of START_ARRAY token

  12. 12

    Can not deserialize instance of Object out of START_ARRAY token

  13. 13

    AWS Can not deserialize instance of java.lang.String out of START_OBJECT

  14. 14

    Json Mapping Exception can not deserialize instance out of START_ARRAY token

  15. 15

    Can not deserialize instance of JsonGen out of START_ARRAY token : newbee

  16. 16

    Can not deserialize instance of my package out of START_ARRAY token

  17. 17

    Can not deserialize instance of SaleListDTO out of START_ARRAY token

  18. 18

    Java REST API: Can not deserialize instance of Object out of START_ARRAY token

  19. 19

    com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token

  20. 20

    Java REST API: Can not deserialize instance of Object out of START_ARRAY token

  21. 21

    Can not deserialize instance of java.util.HashMap out of START_ARRAY token

  22. 22

    Ignore the "Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token" error

  23. 23

    com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token - JAVA

  24. 24

    com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of org.springframework.data.domain.Sort out of START_ARRAY token

  25. 25

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

  26. 26

    com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of Object out of START_ARRAY token

  27. 27

    ObjectMapper fails to deserialize - cannot deserialize instance of .... out of START_ARRAY token

  28. 28

    Jackson Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token

  29. 29

    Jackson error: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token

HotTag

Archive