使用d3js Country JSON的Jackson ObjectMapper转换问题

时间:2014-04-07 09:38:41

标签: json jackson json.net jackson-modules

我想使用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 个答案:

答案 0 :(得分: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个对象。