I need to get the last element in a JSONObject, to retrieve the object my code is:
val jsonObjectRequest = JsonObjectRequest(Request.Method.GET, url, null,
Response.Listener { response ->
Log.i("response", "Response: %s".format(response.toString()))
var jsona: JSONObject = response.getJSONObject("daily")
},
Response.ErrorListener { error ->
Log.i("error", "Error: %s".format(error.toString()))
}
)
And so I acquire the daily value with the jsona
variable
{"1582934400000":12450,"1583020800000":12639,"1583107200000":12439,"1583193600000":12348,"1583280000000":12278,"1583366400000":12322,"1583452800000":12435}
Is there an intelligent way to parse this data to find the value ("1583452800000":12435)
or more preferably the 12435
?
It seems like I can only access data within the object by knowing the string, but the string should change and I only ever need that last data point.
Getting the element with the biggest key (also last data point):
var top: Long = 0
for (key in jsona.keys()){
if (key.toLong() > top) {
top = key.toLong()
}
}
Then accessing it with
jsona.get(top.toString())
Just was hoping for a more efficient solution.