Can't figure out how to get string of JSON response. From OpenWeatherMap API ( https://samples.openweathermap.org/data/2.5/forecast?id=524901&appid=b6907d289e10d714a6e88b30761fae22 ) want to get the first temp_min and temp_max.
I have tried to post the JSON response in a JSON formatter and go through it logically but that didn't help me much. Tried different solutions throughout google.
The last try from my side is this:
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("list");
JSONObject firstObject = (JSONObject)array.get(0);
String tempmax = firstObject.getJSONObject("main").getString("temp_max");
String tempmin = firstObject.getJSONObject("main").getString("temp_min");
From the following API response, I want to receive temp_min and temp_max:
{
"cod":"200",
"message":0.0032,
"cnt":36,
"list":[
{
"dt":1487246400,
"main":{
"temp":286.67,
"temp_min":281.556,
"temp_max":286.67,
"pressure":972.73,
"sea_level":1046.46,
"grnd_level":972.73,
"humidity":75,
"temp_kf":5.11
},
"weather":[ ],
"clouds":{ },
"wind":{ },
"sys":{ },
"dt_txt":"2017-02-16 12:00:00"
},
[..]
I expect to get the temp_min and temp_max values from API response, but at the moment it's just empty.
(String)firstObject.getJSONObject("main").getString("temp_max");I also suggest to add try catch to your code as if there is an error you are not capturing it