我正在尝试使用Jackson 2解组以下JSON文件:
{
"mapID": "123",
"objects": [
{
"mapID": "123",
"objectID": "12",
"properties": {
"type": "2",
"maxSpeed": "110",
"name": "name1",
"bridge": false
},
"geometry": {
"coordinates": [
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
}
]
}
},
{
"mapID": "123",
"objectID": "14",
"properties": {
"type": "5",
"name": "name2",
"redLightTime": "40"
},
"geometry": {
"coordinates": [
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
}
]
}
},
{
"mapID" : "123",
"objectID" : "13",
"properties" : {
"type" : "4",
"maxSpeed" : "40",
"name" : "name3",
"roundaboutLanes" : "2"
},
"geometry": {
"coordinates" : [
[
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
}
]
]
}
}
]
}
public class MapJSON {
private int mapID;
private List<Objects> objects;
public int getMapID() {
return mapID;
}
public void setMapID(int mapID) {
this.mapID = mapID;
}
public List<Objects> getObjects() {
return objects;
}
public void setObjects(List<Objects> objects) {
this.objects = objects;
}
}
public class Objects {
private int mapID;
private int objectID;
private Properties properties;
private Geometry geometry;
public int getMapID() {
return mapID;
}
public void setMapID(int mapID) {
this.mapID = mapID;
}
public int getObjectID() {
return objectID;
}
public void setObjectID(int objectID) {
this.objectID = objectID;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Geometry getGeometry() {
return geometry;
}
public void setGeometry(Geometry geometry) {
this.geometry = geometry;
}
}
public class Geometry {
private List<Coordinates> coordinates;
public List<Coordinates> getCoordinates() {
return coordinates;
}
public void setCoordinates(List<Coordinates> coordinates) {
this.coordinates = coordinates;
}
}
public class Coordinates {
private Double latitude;
private Double longitude;
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
解组Geometry / coordinates元素时发生错误。 有人能指出我的错误在哪里吗?
几何部分之前一切正常。
答案 0 :(得分:2)
在json示例中coordinates
是double数组的数组,但在java代码中它是对象数组:
您需要将JSON调整为格式如下:
"coordinates": [
{
latitude : 4.54559326171875,
longitude : 45.754109791149865
}
]
或者考虑更改您Coordinates
类以反映JSON结构:
public class Coordinates {
private List<Double> coordinates;
}
答案 1 :(得分:1)
您的课程与您的JSON不符。
您的Coordinates
课程有两个双重字段,latitude
和longitude
,它们会匹配如下所示的JSON:
"geometry": {
"coordinates": [
{
"latitude" : 4.54559326171875,
"longitude" : 45.754109791149865
},
...
另一方面,您的JSON将坐标定义为一堆嵌套数组:
"geometry": {
"coordinates": [
[
4.54559326171875,
45.754109791149865
],
...
您需要更改JSON以传入命名字段或更改 你的几何类来存储一个双重嵌套列表:
public class Geometry {
private List<List<Double>> coordinates;
...