gpt4 book ai didi

android - Klaxon 没有在 JSON 中读取 JSONObject

转载 作者:行者123 更新时间:2023-12-04 07:22:50 25 4
gpt4 key购买 nike

我有一个以这种格式返回 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])
这应该是一项简单的任务,但需要花费数小时才能弄清楚。

最佳答案

添加一个 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
}
}
}

关于android - Klaxon 没有在 JSON 中读取 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68386967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com