我正在尝试使用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...
]
}
我需要一个Java类,可以与Gson对象相关联以便使用,但我在处理一组具有不同变量“坐标”的相似对象时遇到了困难。 我有以下等效代码:
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
变成抽象类并使用MultiPolygon
和Polygon
的子类时,Gson
尝试实例化一个Shape
并出现错误。
我可以使用泛型或其他巧妙的方法来解决这个问题吗?