使用 Jackson & message 解析 JSON 文件中的内容时出现的问题 - JsonMappingException - 无法反序列化为超出START_ARRAY令牌
给定以下 .json 文件:
[
{
"name" : "New York",
"number" : "732921",
"center" : [
"latitude" : 38.895111,
"longitude" : -77.036667
]
},
{
"name" : "San Francisco",
"number" : "298732",
"center" : [
"latitude" : 37.783333,
"longitude" : -122.416667
]
}
]
我准备了两个类来表示包含的数据:
public class Location {
public String name;
public int number;
public GeoPoint center;
}
...
public class GeoPoint {
public double latitude;
public double longitude;
}
为了解析.json文件中的内容,我使用Jackson 2.2.x并准备了以下方法:
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;
}
只要我省略了属性,所有内容都可以被解析。但是,当我尝试解析地理坐标时,我最终收到以下错误消息:center
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]
(通过引用链: com.example.Location[“center”])