使用 Jackson & 解析 JSON 文件中的内容时出现问题消息- JsonMappingException - 无法反序列化为超出 START_ARRAY 令牌 [英] Issue with parsing the content from JSON file with Jackson & message- JsonMappingException -Cannot deserialize as out of START_ARRAY token
问题描述
给定以下 .json 文件:
<预><代码>[{"name" : "纽约",号码":732921",中央" : [纬度":38.895111,经度":-77.036667]},{"name" : "旧金山",号码":298732",中央" : [纬度":37.783333,经度":-122.416667]}]我准备了两个类来表示所包含的数据:
public class Location {公共字符串名称;公众号;公共 GeoPoint 中心;}
...
公共类 GeoPoint {公共双纬度;公共双经度;}
为了解析 .json 文件中的内容,我使用 Jackson 2.2.x 并准备了以下方法:>
公共静态列表<位置>getLocations(InputStream inputStream) {ObjectMapper objectMapper = new ObjectMapper();尝试 {TypeFactory typeFactory = objectMapper.getTypeFactory();CollectionType collectionType = typeFactory.constructCollectionType(List.class, Location.class);返回 objectMapper.readValue(inputStream, collectionType);} catch (IOException e) {e.printStackTrace();}返回空;}
只要我省略了 center
属性,就可以解析所有内容.但是,当我尝试解析地理坐标时,我最终收到以下错误消息:
com.fasterxml.jackson.databind.JsonMappingException:无法反序列化
的实例com.example.GeoPoint 在 [来源:android.content.res.AssetManager$AssetInputStream@416a5850; 的 START_ARRAY 令牌之外;第 5 行,第 25 列]
(通过参考链:com.example.Location["center"])
您的 JSON 字符串格式错误:center
的类型是无效对象数组.在 longitude
周围的 JSON 字符串中将 [
和 ]
替换为 {
和 }
和latitude
所以它们将是对象:
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"])
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
}
}
]
这篇关于使用 Jackson & 解析 JSON 文件中的内容时出现问题消息- JsonMappingException - 无法反序列化为超出 START_ARRAY 令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!