I have an API that returns a JSON object in this format
{
"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
}
]
}
As you can see, the "geojson" property is a JSON in itself, it comes from a JSON column in a PostgreSQL database.
The issue comes when reading it with klaxon.
I have the following class and constructor:
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
}
}
and I'm reading the result from the API like this
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)
...
Even though all the other fields are filled, the geojson one is not. How can I fix this? I have tried changing the data type to google's JsonObject, to Klaxon's JsonObject and nothing seems to work, the field just stays empty. This is what appears in my Logs:
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])
This should be a simple task but it's been taking hours to figure.
Add a Geo class, and 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
}
}
}