Java 使用Jackson 2解组json时出错
我正在尝试使用Jackson 2解组以下JSON文件:Java 使用Jackson 2解组json时出错,java,json,jackson,unmarshalling,jackson2,Java,Json,Jackson,Unmarshalling,Jackson2,我正在尝试使用Jackson 2解组以下JSON文件: { "mapID": "123", "objects": [ { "mapID": "123", "objectID": "12", "properties": { "type": "2", "maxSpeed": "110", "name": "name1", "bridge": false }, "
{
"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;
}
{
“mapID”:“123”,
“对象”:[
{
“mapID”:“123”,
“objectID”:“12”,
“财产”:{
“类型”:“2”,
“maxSpeed”:“110”,
“名称”:“名称1”,
“桥”:假
},
“几何学”:{
“坐标”:[
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
}
]
}
},
{
“mapID”:“123”,
“objectID”:“14”,
“财产”:{
“类型”:“5”,
“名称”:“名称2”,
“红光时间”:“40”
},
“几何学”:{
“坐标”:[
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
}
]
}
},
{
“mapID”:“123”,
“objectID”:“13”,
“财产”:{
“类型”:“4”,
“maxSpeed”:“40”,
“名称”:“名称3”,
“环岛线”:“2”
},
“几何学”:{
“坐标”:[
[
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
},
{
“纬度”:4.54559326171875,
“经度”:45.754109791149865
}
]
]
}
}
]
}
公共类MapJSON{
私有int mapID;
私有列表对象;
public int getMapID(){
返回mapID;
}
公共void setMapID(int-mapID){
this.mapID=mapID;
}
公共列表getObjects(){
归还物品;
}
公共void集合对象(列出对象){
this.objects=对象;
}
}
公共类对象{
私有int mapID;
私有int objectID;
私人物业;;
私人几何;
public int getMapID(){
返回mapID;
}
公共void setMapID(int-mapID){
this.mapID=mapID;
}
public int getObjectID(){
返回objectID;
}
public void setObjectID(int objectID){
this.objectID=objectID;
}
公共属性getProperties(){
归还财产;
}
公共void集合属性(属性){
这个。属性=属性;
}
公共几何体getGeometry(){
返回几何;
}
公共空心集几何图形(几何图形){
这个几何=几何;
}
}
公共课几何{
私人名单协调;
公共列表getCoordinates(){
返回坐标;
}
公共空间坐标(列表坐标){
这个。坐标=坐标;
}
}
公共类坐标{
私人双纬度;
私人双经度;
公共双纬度(){
返回纬度;
}
公共纬度(双纬度){
这个。纬度=纬度;
}
公共双getLongitude(){
返回经度;
}
公共经度(双经度){
这个经度=经度;
}
解组几何图形/坐标元素时出错。
有人能告诉我错误在哪里吗
几何体部分之前一切正常。您的类与JSON不匹配 您的
坐标
类有两个双字段,纬度
和经度
,这两个字段将匹配如下所示的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;
...
公共类几何{
私人名单协调;
...
在json示例中,坐标是double数组的数组,但在java代码中是对象数组:
您需要将JSON调整为如下格式:
"coordinates": [
{
latitude : 4.54559326171875,
longitude : 45.754109791149865
}
]
或考虑更改您的代码>坐标< /代码>类,以反映JSON结构:
public class Coordinates {
private List<Double> coordinates;
}
公共类坐标{
私人名单协调;
}
谢谢。我已经在你的评论中更新了我的JSON,但仍然出现了一个错误:cause