给定以下.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
]
}
]
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;
}
以便从中解析内容.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;
}
只要我go 掉center
属性,所有内容都可以被解析.然而,当我试图解析地理坐标时,我最终得到了以下错误消息:
通用域名格式.fasterxml.杰克逊.数据绑定.JsonMappingException:无法反序列化的实例