【问题标题】:Moshi - Parsing JSON with dynamic parametersMoshi - 使用动态参数解析 JSON
【发布时间】: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


    【解决方案1】:

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

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

    该库遵循 GeoJson 格式的严格规则,并且每个不属于 geojson 格式的参数都放入“外来”HashMap

    因为我现在已经在我的外国 hashmap 中做了什么,所以我只需要它们并在需要它们的地方使用它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 2021-04-24
      • 2019-04-24
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多