【问题标题】:Error while unmarshalling json with Jackson 2使用 Jackson 2 解组 json 时出错
【发布时间】:2016-09-27 14:12:14
【问题描述】:

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

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

几何部分之前一切正常。

【问题讨论】:

    标签: java json jackson unmarshalling jackson2


    【解决方案1】:

    在 json 示例中 coordinates 是双精度数组,但在 java 代码中它是对象数组:

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

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

    或考虑更改您的Coordinates 类以反映 JSON 结构:

    public class Coordinates {
        private List<Double> coordinates;
    }
    

    【讨论】:

    • 谢谢。我已经在您的评论中更新了我的 JSON,但我仍然收到错误:原因:com.fasterxml.jackson.databind.JsonMappingException:无法反序列化 fr.enssat.lanniontech.Coordinates 的实例 [来源: src/test/resources/map_all.json;行:62,列:11](通过引用链:fr.enssat.lanniontech.MapJSON["objects"]->java.util.ArrayList[2]->fr.enssat.lanniontech.Objects["geometry"]- >fr.enssat.lanniontech.Geometry[“坐标”]
    • 像这样更改Coordinates 仍然无法正确解析。在 JSON 中,“geometry”有一个“coordinates”字段,但每个子数组不也叫“coordinates”。
    • @azurefrog 所以我真的不明白如何定义我的 JSON 或我的 Java 对象:/
    【解决方案2】:

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

    您的 Coordinates 类有两个双字段,latitudelongitude,它们将匹配如下所示的 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;
        ...
    

    【讨论】:

    • 谢谢。我已经在您的评论中更新了我的 JSON,但我仍然收到错误:原因:com.fasterxml.jackson.databind.JsonMappingException:无法反序列化 fr.enssat.lanniontech.Coordinates 的实例 [来源: src/test/resources/map_all.json;行:62,列:11](通过引用链:fr.enssat.lanniontech.MapJSON["objects"]->java.util.ArrayList[2]->fr.enssat.lanniontech.Objects["geometry"]- >fr.enssat.lanniontech.Geometry[“坐标”]
    • 看起来您已经更新了代码以匹配 Tionio 的答案,而不是我的。如果您像我建议的那样更改 Geometry,则没有要反序列化的 Coordinates 类,因此您不会收到错误消息“无法反序列化 fr.enssat.lanniontech.Coordinates 的实例”
    • 我更新了 JSON,而不是 Java 对象。我现在有我的“几何”:{“坐标”:[{“纬度”:4.54559326171875,“经度”:45.754109791149865},...
    • 那我不知道该告诉你什么;它对我有用。
    • 你测试了吗?你能把 projet Java 文件和 JSON 发给我吗?我想我错过了什么!
    猜你喜欢
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2022-11-28
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多