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;
    }

Geometry/coordinates 要素の非整列化中にエラーが発生します。誰かがエラーの場所を教えてもらえますか?

ジオメトリ部分の前はすべて問題ありません。

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には 2 つの double フィールドと がlatitudeありlongitude、次のような JSON に一致します。

  "geometry": {
    "coordinates": [
      {
        "latitude" : 4.54559326171875,
        "longitude" : 45.754109791149865
      },
      ...

一方、JSON には、ネストされた配列の束として定義された座標があります。

  "geometry": {
    "coordinates": [
      [
        4.54559326171875,
        45.754109791149865
      ],
      ...

名前付きフィールドを渡すように JSON を変更するか、double のネストされたリストを格納するようにジオメトリ クラスを変更する必要があります。

public class Geometry {

    private List<List<Double>> coordinates;
    ...
于 2016-09-27T14:22:04.370 に答える