改造2-如何从复杂的JSON中获取数据

时间:2017-04-19 01:15:13

标签: android json list gson retrofit2

我想从列表中检索数据,但我也不能。我试过这样的事:

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;
        }
    }
}

}

2 个答案:

答案 0 :(得分:1)

您需要将这些POJO类分开。使其成为单独的file.java然后使用ForecastInfor.java访问您的数据

示例:response.getList()。get(0).getTemp()。getMax();

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 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 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 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;
    }
}

答案 1 :(得分:0)

将响应主体强制转换为模型类ForecastInfor。

 ForecastInfor responseBean = null;
            responseBean = (ForecastInfor) response.body();
Float t = responseBean.getList().get(0).getTemp().getMax();

然后为JSON模型创建单独的类,如下所示。

public class CityBean {

private String name;
private String country;
private Float lon;
private Integer geonameId;
private String iso2;
private Float lat;
private String type;
private Integer 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 Float getLon() {
return lon;
}

public void setLon(Float lon) {
this.lon = lon;
}

public Integer getGeonameId() {
return geonameId;
}

public void setGeonameId(Integer geonameId) {
this.geonameId = geonameId;
}

public String getIso2() {
return iso2;
}

public void setIso2(String iso2) {
this.iso2 = iso2;
}

public Float getLat() {
return lat;
}

public void setLat(Float lat) {
this.lat = lat;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Integer getPopulation() {
return population;
}

public void setPopulation(Integer population) {
this.population = population;
}

}

public class ForecastInfor {

private CityBean city;
private Integer message;
private List<ListBean> list = null;
private Integer cod;
private Integer cnt;

public CityBean getCity() {
return city;
}

public void setCity(CityBean city) {
this.city = city;
}

public Integer getMessage() {
return message;
}

public void setMessage(Integer message) {
this.message = message;
}

public List<ListBean> getList() {
return list;
}

public void setList(List<ListBean> list) {
this.list = list;
}

public Integer getCod() {
return cod;
}

public void setCod(Integer cod) {
this.cod = cod;
}

public Integer getCnt() {
return cnt;
}

public void setCnt(Integer cnt) {
this.cnt = cnt;
}

}

public class ListBean {

private Integer clouds;
private TempBean temp;
private Float snow;
private Integer humidity;
private Float pressure;
private List<WeatherBean> weather = null;
private Integer dt;
private Float speed;
private Integer deg;

public Integer getClouds() {
return clouds;
}

public void setClouds(Integer clouds) {
this.clouds = clouds;
}

public TempBean getTemp() {
return temp;
}

public void setTemp(TempBean temp) {
this.temp = temp;
}

public Float getSnow() {
return snow;
}

public void setSnow(Float snow) {
this.snow = snow;
}

public Integer getHumidity() {
return humidity;
}

public void setHumidity(Integer humidity) {
this.humidity = humidity;
}

public Float getPressure() {
return pressure;
}

public void setPressure(Float pressure) {
this.pressure = pressure;
}

public List<WeatherBean> getWeather() {
return weather;
}

public void setWeather(List<WeatherBean> weather) {
this.weather = weather;
}

public Integer getDt() {
return dt;
}

public void setDt(Integer dt) {
this.dt = dt;
}

public Float getSpeed() {
return speed;
}

public void setSpeed(Float speed) {
this.speed = speed;
}

public Integer getDeg() {
return deg;
}

public void setDeg(Integer deg) {
this.deg = deg;
}

}

public class TempBean {

private Float min;
private Float max;
private Float eve;
private Float morn;
private Float night;
private Float day;

public Float getMin() {
return min;
}

public void setMin(Float min) {
this.min = min;
}

public Float getMax() {
return max;
}

public void setMax(Float max) {
this.max = max;
}

public Float getEve() {
return eve;
}

public void setEve(Float eve) {
this.eve = eve;
}

public Float getMorn() {
return morn;
}

public void setMorn(Float morn) {
this.morn = morn;
}

public Float getNight() {
return night;
}

public void setNight(Float night) {
this.night = night;
}

public Float getDay() {
return day;
}

public void setDay(Float day) {
this.day = day;
}

}

public class WeatherBean {

private String main;
private Integer id;
private String icon;
private String description;

public String getMain() {
return main;
}

public void setMain(String main) {
this.main = main;
}

public Integer getId() {
return id;
}

public void setId(Integer 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;
}

}