Проблема преобразования Jackson ObjectMapper с помощью d3js Country JSON
Я хочу десериализовать этот список JSON из Github с помощью Jackson API.
Это вызывает исключение, когда начинает использовать массив внутри массива. Может ли любое тело помочь освоить это картирование.
{
"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();
}
}
}
1 ответ
Решение
Ваш POJO
структура не действительна. Вы можете попробовать с ниже:
class RootMap {
private String type;
private Objects objects;
private List<List<List<Integer>>> arcs;
// getters, setters, other
}
class Objects {
private Countries countries;
// getters, setters, other
}
class Countries {
private String type;
private List<BigDecimal> bbox;
private List<Geometry> geometries;
// getters, setters, other
}
class Geometry {
private String id;
private String type;
private List<Object> arcs;
// getters, setters
}
Выше структура не содержит все свойства (но я думаю, что вы сможете добавить недостающие), поэтому мы должны включить DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
особенность десериализации нашего JSON
, Пример использования:
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
System.out.println(mapper.readValue(json, RootMap.class));
Выше код печатает наш POJO
объекты.