2017-04-19 4 views
2

Ich möchte die Daten aus der Liste abrufen, aber ich bin nicht in der Lage. Ich habe wie folgt versucht:Nachrüsten 2- Wie bekomme ich Daten von komplizierten JSON

double t=response.body().getList().get(0).getTemp().getMax(); 

Aber ich bekomme eine Null-Zeiger-Ausnahme

http://www.json-generator.com/api/json/get/cuDhZDicMO?indent=2

Das ist meine JSON ist.

Dies ist das Datenmodell. Wie kann ich die Daten in der Liste

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

}

+0

Mögliche Duplikat [Retrofit2 Daten Artikelnummer- etwas falsch mit der Liste] (http://stackoverflow.com/questions/43472796/retrofit2-data-model-something-wrong-with-the-list) –

+0

Sind Sie wirklich sicher, dass die Antwort genau das ist, was Sie angegeben haben? Es sieht so aus, als wäre die 'liste' in Ihrem _real_ JSON entweder 'null' oder nicht definiert. –

+0

@ Lyubomyr Shaydariv, aber ich bin in der Lage, einige Informationen, die nicht in der Liste von der Antwort ist. Das heißt, es funktioniert –

Antwort

1

Sie müssen diese POJO Klassen trennen abrufen. machen Sie es individuell file.java dann verwenden ForecastInfor.java Zugriff auf Ihre Daten

Beispiel: 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; 
    } 
} 
+0

Ich habe es gerade versucht. Getrennt die Klassen und verwendet --- double t = response.body(). GetList(). Get (0) .getTemp(). GetMax(); Der Fehler -> java.lang.NullPointerException: Versuch, die Interface-Methode 'java.lang.Object java.util.List.get (int)' für eine Nullobjekt-Referenz aufzurufen –

0

Übergeben Sie den Antworttext an die Modellklasse ForecastInfor wie folgt.

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

Dann separate Klassen für das JSON Modell wie folgt erstellen.

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

} 
+0

Es gibt mir denselben Fehler für die Zeile - --- responseBean = (ForecastInfor) response.body(); --- java.lang.NullPointerException: Es wurde versucht, die Interface-Methode 'java.lang.Object java.util.List.get (int)' für eine Nullobjekt-Referenz aufzurufen –

Verwandte Themen