我正在尝试使用Gson解析GeoJSON,并且我有一个看起来像这样的 JSON(简化):
{"type": "FeatureCollection",
"features": [
{ "type": "Feature", "name": "Afghanistan", "geometry":
{ "type": "Polygon", "coordinates":
<<a 3-dimensional double array (double[][][] in Java)>>
}
},
{ "type": "Feature", "name": "Indonesia", "geometry":
{ "type": "MultiPolygon", "coordinates":
<<a 4-dimensional double array (double[][][][] in Java)>>
}
},
//etc...
]
}
我需要有一个可以与 Gson 对象相关联的 java 类才能使用它,但我正在努力处理变量coordinates
不相同的类似对象数组。我有相当于:
class FeatureCollection{
String type;
Feature[] features;
}
class Feature{
String type,name;
Shape geometry;
}
class Shape{
String type;
??? coordinates;
}
- 当我尝试使用
double[][][]
而不是???
,我得到一个com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a double but was BEGIN_ARRAY at line 6 column 146
- 当我尝试创建
Shape
一个抽象类并使用 and 的子类时MultiPolygon
,Polygon
尝试Gson
实例化 aShape
and 错误。
我可以使用泛型或其他鬼鬼祟祟的东西来解决这个问题吗?