I want to deserialize this JSON list from Github using Jackson API.
It is causing exception when started to use the array inside array. Can any any body help to learn this Mapping .
{
"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 Code : 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
}
}
Also Test Class
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();
}
}
}
Your POJO
structure is not valid. You can try with below:
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
}
Above structure does not contain all properties (but I believe you will be able to add missing) so we have to enable DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
feature to deserialize our JSON
.
Example usage:
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
System.out.println(mapper.readValue(json, RootMap.class));
Above code prints our POJO
objects.