Moshi - 使用动态参数解析 JSON

Moshi - Parsing JSON with dynamic parameters

我有这部分 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 解析它,但我不知道从哪里开始。

这里的关键信息是: 模型 classes 必须基于 type 字段构建。

为了使其成为动态的,我想创建一个 GeoJSONType 数据 class,这将是 distancePoints 和叠加层的类型。例如:

data class Object1(
val distancePoints: GeoJSONType,
val overlays: GeoJSONType)

data class GeoJSONType(parameters here)

将从 GeoJSONType 创建的对象将是使用“坐标”值的简单数据 classes。例如:

data class Point()
data class LineString()

等等

解决这个问题的正确方法是什么。

提前致谢。

好吧,经过很长时间的研究,我使用 scruffyfox 的库解决了这个问题:https://github.com/scruffyfox/Android-GeoGson

他已经使用 geojson 和 moshi 专门为这个用例创建了所有模型和适配器,所以我只是复制它们,修改它们以用于我的自定义用例,现在它完美地工作了。

该库遵循严格的 GeoJson 格式规则,每个不属于 geojson 格式的参数都放在“外部”HashMap 中。

因为我现在已经知道我的外国 hashmap 中会有什么,所以我只是把它们拿走并在需要它们的任何地方使用它们。