JsonMappingException: out of START_ARRAY token

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

Given the following .json file:

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

I prepared two classes to represent the contained data:

public class Location {     public String name;     public int number;     public GeoPoint center; } 

...

public class GeoPoint {     public double latitude;     public double longitude; } 

In order to parse the content from the .json file I use Jackson 2.2.x and prepared the following method:

public static List<Location> getLocations(InputStream inputStream) {     ObjectMapper objectMapper = new ObjectMapper();     try {         TypeFactory typeFactory = objectMapper.getTypeFactory();         CollectionType collectionType = typeFactory.constructCollectionType(                                             List.class, Location.class);         return objectMapper.readValue(inputStream, collectionType);     } catch (IOException e) {         e.printStackTrace();     }     return null; } 

As long as I leave out the center property all content can be parsed. However, when I try to parse the geo-coordinates I end up with the following error message:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of
com.example.GeoPoint out of START_ARRAY token at [Source: android.content.res.AssetManager$AssetInputStream@416a5850; line: 5, column: 25]
(through reference chain: com.example.Location["center"])

回答1:

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             }     } ] 


回答2:

JsonMappingException: out of START_ARRAY token exception is thrown by Jackson object mapper as it's expecting an Object {} whereas it found an Array [{}] in response.

This can be solved by replacing Object with Object[] in the argument for geForObject("url",Object[].class). References:

  1. Ref.1
  2. Ref.2
  3. Ref.3


回答3:

As said, JsonMappingException: out of START_ARRAY token exception is thrown by Jackson object mapper as it's expecting an Object {} whereas it found an Array [{}] in response.

A simpler solution could be replacing the method getLocations with:

public static List<Location> getLocations(InputStream inputStream) {     ObjectMapper objectMapper = new ObjectMapper();     try {         TypeReference<List<Location>> typeReference = new TypeReference<>() {};         return objectMapper.readValue(inputStream, typeReference);     } catch (IOException e) {         e.printStackTrace();     }     return null; } 

On the other hand, if you don't have a pojo like Location, you could use:

TypeReference<List<Map<String, Object>>> typeReference = new TypeReference<>() {}; return objectMapper.readValue(inputStream, typeReference); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!