JJD*_*JJD 85 java arrays json jackson
给出以下.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
]
}
]
Run Code Online (Sandbox Code Playgroud)
我准备了两个类来表示包含的数据:
public class Location {
public String name;
public int number;
public GeoPoint center;
}
Run Code Online (Sandbox Code Playgroud)
...
public class GeoPoint {
public double latitude;
public double longitude;
}
Run Code Online (Sandbox Code Playgroud)
为了解析.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;
}
Run Code Online (Sandbox Code Playgroud)
只要我遗漏了center
属性,就可以解析所有内容.但是,当我尝试解析地理坐标时,我最终得到以下错误消息:
com.fasterxml.jackson.databind.JsonMappingException:无法
在[Source:android.content.res.AssetManager$AssetInputStream@416a5850;来源:START_ARRAY令牌中反序列化com.example.GeoPoint的实例.line:5,column:25]
(通过引用链:com.example.Location ["center"])
Kat*_*ona 66
您的JSON字符串格式错误:类型center
是无效对象的数组.替换[
和]
使用{
和}
在JSON字符串中longitude
,latitude
因此它们将是对象:
[
{
"name" : "New York",
"number" : "732921",
"center" : {
"latitude" : 38.895111,
"longitude" : -77.036667
}
},
{
"name" : "San Francisco",
"number" : "298732",
"center" : {
"latitude" : 37.783333,
"longitude" : -122.416667
}
}
]
Run Code Online (Sandbox Code Playgroud)
如前所述,JsonMappingException: out of START_ARRAY token
Jackson对象映射器会抛出异常,因为它期望an Object {}
而它却找到了Array [{}]
响应。
一个更简单的解决方案是将方法替换为getLocations
:
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;
}
Run Code Online (Sandbox Code Playgroud)
另一方面,如果您没有喜欢的pojo Location
,则可以使用:
TypeReference<List<Map<String, Object>>> typeReference = new TypeReference<>() {};
return objectMapper.readValue(inputStream, typeReference);
Run Code Online (Sandbox Code Playgroud)
我将这个问题排序为从JSONLint.com验证json,然后更正它。这是相同的代码。
String jsonStr = "[{\r\n" + "\"name\":\"New York\",\r\n" + "\"number\": \"732921\",\r\n"+ "\"center\": {\r\n" + "\"latitude\": 38.895111,\r\n" + " \"longitude\": -77.036667\r\n" + "}\r\n" + "},\r\n" + " {\r\n"+ "\"name\": \"San Francisco\",\r\n" +\"number\":\"298732\",\r\n"+ "\"center\": {\r\n" + " \"latitude\": 37.783333,\r\n"+ "\"longitude\": -122.416667\r\n" + "}\r\n" + "}\r\n" + "]";
ObjectMapper mapper = new ObjectMapper();
MyPojo[] jsonObj = mapper.readValue(jsonStr, MyPojo[].class);
for (MyPojo itr : jsonObj) {
System.out.println("Val of name is: " + itr.getName());
System.out.println("Val of number is: " + itr.getNumber());
System.out.println("Val of latitude is: " +
itr.getCenter().getLatitude());
System.out.println("Val of longitude is: " +
itr.getCenter().getLongitude() + "\n");
}
Run Code Online (Sandbox Code Playgroud)
注意:MyPojo[].class
是具有json属性的getter和setter的类。
结果:
Val of name is: New York
Val of number is: 732921
Val of latitude is: 38.895111
Val of longitude is: -77.036667
Val of name is: San Francisco
Val of number is: 298732
Val of latitude is: 37.783333
Val of longitude is: -122.416667
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
256570 次 |
最近记录: |