我想使用Jackson API从 Github反序列化这个JSON列表。
开始使用数组内的数组时会导致异常。任何人都可以帮助学习此映射。
{
"type":"Topology",
"objects":{
"countries":{
"bbox":[
-179.99999999999986,
-55.52450937299982,
180.00000000000014,
83.61347077000005
],
"type":"GeometryCollection",
"geometries":[
{
"type":"Polygon",
"properties":{
"name":"Afghanistan"
},
"id":"AFG",
"arcs":[
[
0,
1,
2,
3,
4,
5
]
]
},
{
"type":"MultiPolygon",
"properties":{
"name":"Angola"
},
"id":"AGO",
"arcs":[
[
[
6,
7,
8,
9
]
],
[
[
10,
11,
12
]
]
]
},
{
"type":"Polygon",
"properties":{
"name":"Albania"
},
"id":"ALB",
"arcs":[
[
13,
14,
15,
16,
17,
18,
19,
20
]
]
},
{
"type":"Polygon",
"properties":{
"name":"Aland"
},
"id":"ALD",
"arcs":[
[
21
]
]
},
{
"type":"Polygon",
"properties":{
"name":"Andorra"
},
"id":"AND",
"arcs":[
[
22,
23
]
]
},
{
"type":"Polygon",
"properties":{
"name":"United Arab Emirates"
},
"id":"ARE",
"arcs":[
[
24,
25,
26,
27,
28
]
]
},
....
}
Java 代码:Pojo
public class Countries {
private List<String> type;
private CountyProperties properties;
private String id;
private List<Object> arcs;
public Countries(List<String> type, CountyProperties properties, String id,
List<Object> arcs) {
super();
this.type = type;
this.properties = properties;
this.id = id;
this.arcs = arcs;
}
public List<String> getType() {
return type;
}
public void setType(List<String> type) {
this.type = type;
}
public CountyProperties getProperties() {
return properties;
}
public void setProperties(CountyProperties properties) {
this.properties = properties;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Object> getArcs() {
return arcs;
}
public void setArcs(List<Object> arcs) {
this.arcs = arcs;
}
public Countries() {
super();
// TODO Auto-generated constructor stub
}
}
也测试类
public class JsonMapperTest {
@Test
public void jsonTransformer(){
ObjectMapper mapper = new ObjectMapper();
try {
Countries user = mapper.readValue(new File("countries.json"), Countries.class);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}