【发布时间】:2020-06-23 09:00:34
【问题描述】:
我的 JSON 中有这部分:
{
"object": [
{
"id": "blablabla",
"name": "object1",
"type": "GeometryCollection",
"distancePoints": {
"type": "FeatureCollection",
"form": "distancePoint",
"features": [
{
"type": "Feature",
"form": "feature",
"geometry": {
"coordinates": [
9.999999999999999,
0.000000000000000
],
"type": "Point"
}
},
{
"type": "Feature",
"form": "feature",
"geometry": {
"coordinates": [
[
2.222222222222222,
4.444444444444444
],
[
3.333333333333333,
5.555555555555555
]
],
"type": "LineString"
}
},
{
"type": "Feature",
"form": "feature",
"geometry": {
"coordinates": [
[
7.777777777777777,
6.666666666666666
],
[
7.777777777777777,
6.666666666666666
],
[
7.777777777777777,
6.666666666666666
]
],
"type": "Polygon"
}
}
]
},
"overlays": {
"type": "FeatureCollection",
"form": "overlay",
"features": [
{
"type": "Feature",
"form": "feature",
"geometry": {
"coordinates": [
[
9.999999999999999,
0.000000000000000
],
[
3.333333333333333,
5.555555555555555
]
],
"type": "LineString"
}
}
]
}
}
]
}
我需要使用 Kotlin 解析这个,但我不知道从哪里开始。
这里的关键信息是: 模型类必须基于 type 字段来构建。
为了让它是动态的,我想创建一个 GeoJSONType 数据类,它是 distancePoints 和覆盖的类型。例如:
data class Object1(
val distancePoints: GeoJSONType,
val overlays: GeoJSONType)
data class GeoJSONType(parameters here)
从 GeoJSONType 创建的对象将是使用“坐标”值的简单数据类。例如:
data class Point()
data class LineString()
等等
解决这个问题的正确方法是什么。
提前致谢。
【问题讨论】:
标签: android json kotlin geojson moshi