1

我正在尝试使用 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;
    }

解组几何/坐标元素时发生错误。有人可以指出错误在哪里吗?

在几何部分之前一切正常。

4

2 回答 2

2

在 json 示例coordinates中是 double 数组的数组,但在 java 代码中它是对象数组:

您需要将 JSON 调整为如下格式:

"coordinates": [
          {
            latitude : 4.54559326171875,
            longitude : 45.754109791149865
          }
        ]

或者考虑改变你Coordinates的类以反映 JSON 结构:

public class Coordinates {
    private List<Double> coordinates;
}
于 2016-09-27T14:32:44.810 回答
1

您的课程与您的 JSON 不匹配。

您的Coordinates类有两个双字段latitudelongitude,它们将匹配如下所示的 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;
    ...
于 2016-09-27T14:22:04.370 回答