2

我有一个以这种格式返回 JSON 对象的 API

{
    "success": true,
    "data": [
        {
            "id": 1,
            "id_instituicao": 4,
            "geojson": {
                "type": "FeatureCollection",
                "features": [
                    {
                        "type": "Feature",
                        "geometry": {
                            "type": "Polygon",
                            "coordinates": [
                                [
                                    [
                                        167.9937252983775,
                                        -28.22733432615155
                                    ],
                                    [
                                        150.2832565483775,
                                        -28.845035838646826
                                    ],
                                    [
                                        147.1191940483775,
                                        -34.01658459366346
                                    ],
                                    [
                                        140.2637252983775,
                                        -28.22733432615155
                                    ]
                                ]
                            ]
                        },
                        "properties": {}
                    },
                    {
                        "type": "Feature",
                        "geometry": {
                            "type": "Polygon",
                            "coordinates": [
                                [
                                    [
                                        136.7133359375,
                                        -32.45329185818206
                                    ],
                                    [
                                        141.5473203125,
                                        -34.86706479052107
                                    ],
                                    [
                                        137.2406796875,
                                        -35.15500848278408
                                    ],
                                    [
                                        136.7133359375,
                                        -32.45329185818206
                                    ]
                                ]
                            ]
                        },
                        "properties": {}
                    }
                ]
            },
            "lat_centro": -31.691171404467816,
            "long_centro": 152.35353061793876,
            "raio_coordenadas": 15.640194680438753,
            "nome": "Cantina",
            "lotacao_max": null,
            "data_criacao": "2021-07-07T16:38:53.607Z",
            "media_classificacao": null
        }
    ]
}

如您所见,“geojson”属性本身就是一个 JSON,它来自 PostgreSQL 数据库中的一个 JSON 列。

当用 klaxon 阅读它时,问题就来了。

我有以下类和构造函数:

class Espaco {
    var id: Int?
    var id_instituicao: Int?
    var geojson: JSONObject
    var nome: String
    var lotacao_max: Int?
    var data_criacao: String?
    var lat_centro: Double?
    var long_centro: Double?
    var raio_coordenadas: Double?
    var media_classificacao: Double?

constructor(id: Int?, id_instituicao: Int?, geojson: JSONObject, lat_centro: Double?, long_centro: Double?, raio_coordenadas:Double?,  nome: String, lotacao_max: Int?, data_criacao: String?, media_classificacao: Double?)
    {
        this.id = id
        this.id_instituicao = id_instituicao
        this.geojson = geojson
        this.nome = nome
        this.lotacao_max = lotacao_max
        this.data_criacao = data_criacao
        this.lat_centro=lat_centro
        this.long_centro=long_centro
        this.raio_coordenadas=raio_coordenadas
        this.media_classificacao = media_classificacao
    }
}

我正在像这样从 API 中读取结果

 data class Resultado(
                    @Json(name = "data")
                    val espacos: ArrayList<Espaco>
                )

                val resultado = Klaxon().parse<Resultado>(StringReader(response))

                resultado!!.espacos.forEach({

                    Log.v("It:", "" + it.lat_centro)
                    Log.v("Geojson:", "" + it.geojson)
                    Log.v("Resultado: ", "" + resultado)
...

即使所有其他字段都已填写,但 geojson 字段并未填写。我怎样才能解决这个问题?我尝试将数据类型更改为 google 的 JsonObject 和 Klaxon 的 JsonObject,但似乎没有任何效果,该字段保持为空。这是我的日志中出现的内容:

2021-07-15 02:34:49.192 25058-25058/com.example.crowdzero V/Result:: {"success":true,"data":[{"id":1,"id_instituicao":4,"geojson":{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[167.9937252983775,-28.22733432615155],[150.2832565483775,-28.845035838646826],[147.1191940483775,-34.01658459366346],[140.2637252983775,-28.22733432615155]]]},"properties":{}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[136.7133359375,-32.45329185818206],[141.5473203125,-34.86706479052107],[137.2406796875,-35.15500848278408],[136.7133359375,-32.45329185818206]]]},"properties":{}}]},"lat_centro":-31.691171404467816,"long_centro":152.35353061793876,"raio_coordenadas":15.640194680438753,"nome":"Cantina","lotacao_max":null,"data_criacao":"2021-07-07T16:38:53.607Z","media_classificacao":null}]}
2021-07-15 02:34:49.244 25058-25058/com.example.crowdzero V/It:: -31.691171404467816
2021-07-15 02:34:49.244 25058-25058/com.example.crowdzero V/Geojson:: {}
2021-07-15 02:34:49.244 25058-25058/com.example.crowdzero V/Resultado:: Resultado(espacos=[com.example.crowdzero.Espaco@2c965d9])

这应该是一项简单的任务,但要花好几个小时才能弄清楚。

4

1 回答 1

1

添加一个 Geo 类和 var geojson: Geo

class Espaco {
    var id: Int?
    var id_instituicao: Int?
    var geojson: Geo
    var nome: String
    var lotacao_max: Int?
    var data_criacao: String?
    var lat_centro: Double?
    var long_centro: Double?
    var raio_coordenadas: Double?
    var media_classificacao: Double?

    constructor(id: Int?, id_instituicao: Int?, geojson: Geo, lat_centro: Double?, long_centro: Double?, raio_coordenadas:Double?,  nome: String, lotacao_max: Int?, data_criacao: String?, media_classificacao: Double?)
    {
        this.id = id
        this.id_instituicao = id_instituicao
        this.geojson = geojson
        this.nome = nome
        this.lotacao_max = lotacao_max
        this.data_criacao = data_criacao
        this.lat_centro=lat_centro
        this.long_centro=long_centro
        this.raio_coordenadas=raio_coordenadas
        this.media_classificacao = media_classificacao
    }

    class Geo {
        var type: String? = null

        var features: List<FeaturesBean>? = null

        class FeaturesBean {
            var type: String? = null

            var geometry: GeometryBean? = null
            var properties: PropertiesBean? = null

            class GeometryBean {
                var type: String? = null
                var coordinates: List<List<List<Double>>>? = null
            }
            class PropertiesBean
        }
    }
}
于 2021-07-15T03:11:24.087 回答