2014-04-07 3 views
0

Я хочу десериализовать этот список JSON из Github, используя Jackson API.Проблема с конверсией ObjectMapper Jackson с d3js Страна 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(); 
     } 
    } 
} 
+0

Пожалуйста, обновите формат –

+0

Не могли бы вы указать исходный код и исключение, которое вы получили? –

+0

thik об использовании этой библиотеки https://github.com/opendatalab-de/geojson-jackson – geogeek

ответ

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 объектов.

+0

Спасибо большое My Michał Ziober, Позвольте мне попробовать с этим – San

Смежные вопросы