我有一个API,它返回这种格式的JSON对象
{
"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列。
当与克拉克森一起阅读它时,问题就来了。
我有以下类和构造函数:
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字段仍未填充。我该如何解决这个问题呢?我试着把数据类型改成谷歌的JsonObject,再改成克拉克森的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])这应该是一项简单的任务,但它需要几个小时才能弄清楚。
发布于 2021-07-15 11:11:24
添加一个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
}
}
}https://stackoverflow.com/questions/68386967
复制相似问题