Jalil Sayed Hassan:
Eu quero converter um JSONObject a um objeto usando Gson. No entanto, o objeto não ter corrigido chaves para determinados itens.
{
"hits": [],
"target": {
"geo": {
"54.57982-24.41563": 891,
"55.37717-25.30446": 725,
"55.47091-25.31749": 569,
"55.20887-25.05958": 514,
"55.45714-25.29926": 494,
"54.68297-24.34772": 406,
"54.55594-24.33671": 314,
"55.42375-25.22124": 295,
"54.55434-24.33302": 277,
"55.25917-25.11189": 266
}
}
}
O objeto ge0, como você pode ver não tem teclas fixas.
Santanu Sur:
U pode substituí-lo com um HashMap
: -
data class Response(val hits: List<Any>, val target: Target) {
data class Target(val geo: HashMap<String, Int>)
}
então deserialize
val response: Response = gson.fromJson(jsonObjectInString.trim(), Response::class.java)
circuito, em seguida, sobre o mapa: -
for((latLong, value) in response.target.geo) {
// work with your keys and values
}