【发布时间】:2017-09-15 00:41:58
【问题描述】:
我想从列表中检索数据,但我也做不到。我试过这样:
double t=response.body().getList().get(0).getTemp().getMax();
但我得到一个空指针异常
http://www.json-generator.com/api/json/get/cuDhZDicMO?indent=2
那是我的 JSON。
这是数据模型。如何检索列表中的数据
public class ForecastInfor {
private CityBean city;
private int message;
private int cod;
private int cnt;
private List<ListBean> list;
public CityBean getCity() {
return city;
}
public void setCity(CityBean city) {
this.city = city;
}
public int getMessage() {
return message;
}
public void setMessage(int message) {
this.message = message;
}
public int getCod() {
return cod;
}
public void setCod(int cod) {
this.cod = cod;
}
public int getCnt() {
return cnt;
}
public void setCnt(int cnt) {
this.cnt = cnt;
}
public List<ListBean> getList() {
return list;
}
public void setList(List<ListBean> list) {
this.list = list;
}
public static class CityBean {
/**
* name : Moscow
* country : RU
* lon : 37.6156
* geoname_id : 524901
* iso2 : RU
* lat : 55.7522
* type : city
* population : 0
*/
private String name;
private String country;
private double lon;
private int geoname_id;
private String iso2;
private double lat;
private String type;
private int population;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public double getLon() {
return lon;
}
public void setLon(double lon) {
this.lon = lon;
}
public int getGeoname_id() {
return geoname_id;
}
public void setGeoname_id(int geoname_id) {
this.geoname_id = geoname_id;
}
public String getIso2() {
return iso2;
}
public void setIso2(String iso2) {
this.iso2 = iso2;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
}
public static class ListBean {
private int clouds;
private TempBean temp;
private double snow;
private int humidity;
private double pressure;
private int dt;
private double speed;
private int deg;
private List<WeatherBean> weather;
public int getClouds() {
return clouds;
}
public void setClouds(int clouds) {
this.clouds = clouds;
}
public TempBean getTemp() {
return temp;
}
public void setTemp(TempBean temp) {
this.temp = temp;
}
public double getSnow() {
return snow;
}
public void setSnow(double snow) {
this.snow = snow;
}
public int getHumidity() {
return humidity;
}
public void setHumidity(int humidity) {
this.humidity = humidity;
}
public double getPressure() {
return pressure;
}
public void setPressure(double pressure) {
this.pressure = pressure;
}
public int getDt() {
return dt;
}
public void setDt(int dt) {
this.dt = dt;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public int getDeg() {
return deg;
}
public void setDeg(int deg) {
this.deg = deg;
}
public List<WeatherBean> getWeather() {
return weather;
}
public void setWeather(List<WeatherBean> weather) {
this.weather = weather;
}
public static class TempBean {
/**
* min : 261.41
* max : 262.65
* eve : 262.65
* morn : 262.65
* night : 261.41
* day : 262.65
*/
private double min;
private double max;
private double eve;
private double morn;
private double night;
private double day;
public double getMin() {
return min;
}
public void setMin(double min) {
this.min = min;
}
public double getMax() {
return max;
}
public void setMax(double max) {
this.max = max;
}
public double getEve() {
return eve;
}
public void setEve(double eve) {
this.eve = eve;
}
public double getMorn() {
return morn;
}
public void setMorn(double morn) {
this.morn = morn;
}
public double getNight() {
return night;
}
public void setNight(double night) {
this.night = night;
}
public double getDay() {
return day;
}
public void setDay(double day) {
this.day = day;
}
}
public static class WeatherBean {
/**
* main : Clear
* id : 800
* icon : 01d
* description : sky is clear
*/
private String main;
private int id;
private String icon;
private String description;
public String getMain() {
return main;
}
public void setMain(String main) {
this.main = main;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
}
}
【问题讨论】:
-
您确定响应与您提供的完全一致吗?看起来
list在您的 real JSON 中要么是null要么是未定义的。 -
@Lyubomyr Shaydariv 但我能够从响应中检索一些不在列表中的信息。所以这意味着它有效
-
您在下面的评论中提到了 NPE。
-
@LyubomyrShaydariv 我能够从 JSON 中检索 cod、message 和 cnt。这三个不在名单上。列表中的所有内容,我无法访问。所以这意味着响应很好,但我只是试图以错误的方式检索它?
标签: android json list gson retrofit2