Не удаётся "Достучаться" до объекта JSON
Есть JSON код:
{
"city": {
"country": "RU",
"coord": {
"lon": 37.6156,
"lat": 55.7522
},
"sunrise": 1589332872,
"timezone": 10800,
"sunset": 1589391041,
"name": "Moscow",
"id": 524901,
"population": 1000000
},
"cnt": 1,
"cod": "200",
"message": 0,
"list": [
{
"dt": 1589392800,
"dt_txt": "2020-05-13 18:00:00",
"weather": [
{
"icon": "04n",
"description": "broken clouds",
"main": "Clouds",
"id": 803
}
],
"main": {
"temp": 9.82,
"temp_min": 9.45,
"grnd_level": 997,
"temp_kf": 0.37,
"humidity": 48,
"pressure": 1012,
"sea_level": 1013,
"feels_like": 4.69,
"temp_max": 9.82
},
"clouds": {
"all": 77
},
"sys": {
"pod": "n"
},
"wind": {
"deg": 242,
"speed": 4.35
}
}
]
}
Как достучаться до объекта "list.main.temp" Я пробовал сделать это с помощью .getJSONObject("list") Но не получилось. При этом к объекту city.country или cnt работает.
Ответы (1 шт):
Автор решения: Circassian
→ Ссылка
Пример того, как парсить json средствами org.json:
String json = "";
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonList = jsonObject.getJSONArray("list");
JSONObject jsonItem = jsonList.getJSONObject(0);
JSONObject jsonMain = jsonItem.getJSONObject("main");
String temp = jsonMain.getString("temp");
Пример того, как парсить json с помощью библиотеки Gson:
Example example = Gson().fromJson(json, Example.class);
Float temp = example.list.get(0).main.temp;
POJO классы:
class Example {
@SerializedName("city")
public City city;
@SerializedName("cnt")
public Integer cnt;
@SerializedName("cod")
public String cod;
@SerializedName("message")
public Integer message;
@SerializedName("list")
public List<WList> list = null;
}
class City {
@SerializedName("country")
public String country;
@SerializedName("coord")
public Coord coord;
@SerializedName("sunrise")
public Integer sunrise;
@SerializedName("timezone")
public Integer timezone;
@SerializedName("sunset")
public Integer sunset;
@SerializedName("name")
public String name;
@SerializedName("id")
public Integer id;
@SerializedName("population")
public Integer population;
}
class Clouds {
@SerializedName("all")
public Integer all;
}
class Coord {
@SerializedName("lon")
public Float lon;
@SerializedName("lat")
public Float lat;
}
class WList {
@SerializedName("dt")
public Integer dt;
@SerializedName("dt_txt")
public String dtTxt;
@SerializedName("weather")
public java.util.List<Weather> weather = null;
@SerializedName("main")
public Main main;
@SerializedName("clouds")
public Clouds clouds;
@SerializedName("sys")
public Sys sys;
@SerializedName("wind")
public Wind wind;
}
class Main {
@SerializedName("temp")
public Float temp;
@SerializedName("temp_min")
public Float tempMin;
@SerializedName("grnd_level")
public Integer grndLevel;
@SerializedName("temp_kf")
public Float tempKf;
@SerializedName("humidity")
public Integer humidity;
@SerializedName("pressure")
public Integer pressure;
@SerializedName("sea_level")
public Integer seaLevel;
@SerializedName("feels_like")
public Float feelsLike;
@SerializedName("temp_max")
public Float tempMax;
}
class Sys {
@SerializedName("pod")
public String pod;
}
class Weather {
@SerializedName("icon")
public String icon;
@SerializedName("description")
public String description;
@SerializedName("main")
public String main;
@SerializedName("id")
public Integer id;
}
class Wind {
@SerializedName("deg")
public Integer deg;
@SerializedName("speed")
public Float speed;
}