无法遍历JSON数组

时间:2019-07-08 06:41:02

标签: java android json api retrofit

我有这个JSON响应

    {
  "cod": "200",
  "message": 0.0085,
  "cnt": 40,
  "list": [
    {
      "dt": 1562533200,
      "main": {
        "temp": 15.55,
        "temp_min": 15.55,
        "temp_max": 15.96,
        "pressure": 1012.01,
        "sea_level": 1012.01,
        "grnd_level": 976.03,
        "humidity": 97,
        "temp_kf": -0.41
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10n"
        }
      ],
      "clouds": {
        "all": 100
      },

,依此类推... 我如何从天气数组中获取数据?我尝试过

    ArrayList<hourlyModel.Weather> list = new ArrayList<>();


 public void onResponse(Call<hourlyModel> call, Response<hourlyModel> response) {
                        if (!response.isSuccessful()){
                                Log.i(TAG, "onResponse: "+response.code());
                        }
                    Log.i(TAG, "onResponse: "+response.code());

                        list = (ArrayList<hourlyModel.Weather>) response.body().getWeather();

                        for (hourlyModel.Weather model : list){
                            forecastID = String.valueOf(model.getForecastIcon());
                            Toast.makeText(Hourly_weather.this,forecastID,Toast.LENGTH_LONG).show();
                        }
                }

这是POJO

 @SerializedName("list")
List<ListPOJO> listList;
@SerializedName("weather")
List<Weather> weather;

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

public List<ListPOJO> getList() {
    return listList;
}

 class ListPOJO{

    @SerializedName("main")
    Main main;

    @SerializedName("wind")
    Wind wind;

    @SerializedName("dt")
    long time;

    public Main getMain() {
        return main;
    }

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

    public Wind getWind() {
        return wind;
    }

    public long getTime() {
        return time;
    }
}

class Main {

    @SerializedName("temp")
    private Double actualTemperature;
    @SerializedName("pressure")
    private Double airPressure;
    @SerializedName("humidity")
    private Double airHumidity;
    @SerializedName("temp_min")
    private Double minTemp;
    @SerializedName("temp_max")
    private Double maxTemp;


    public Double getActualTemperature() {
        return actualTemperature;
    }

    public Double getAirPressure() {
        return airPressure;
    }

    public Double getAirHumidity() {
        return airHumidity;
    }

    public Double getMinTemp() {
        return minTemp;
    }

    public Double getMaxTemp() {
        return maxTemp;
    }

    public Main(Double actualTemperature, Double airPressure, Double airHumidity) {
        this.actualTemperature = actualTemperature;
        this.airPressure = airPressure;
        this.airHumidity = airHumidity;
    //            this.weather.forecastIcon = forecastID;
    }
}

class Wind{
    @SerializedName("speed")
    private Double windSpeed;

    public Double getWindSpeed() {
        return windSpeed;
    }
}


class Weather{
    @SerializedName("icon")
    private String forecastIcon;

    public String getForecastIcon() {
        return forecastIcon;
    }
}

我认为,我想去hourlyModel.ListPOJO->天气,但是有点不行。收到此错误尝试在null对象引用上调用虚拟方法'java.util.Iterator java.util.ArrayList.iterator()',我应该进行哪些更改才能使其起作用?我不明白,为什么我不能从第一个数组到达第二个数组。

4 个答案:

答案 0 :(得分:1)

您的json不是有效的json,您可以使用此网站将有效的json转换为Java对象: http://www.jsonschema2pojo.org/

此外,由于listweather不在同一级别,因此您的pojo不正确。

答案 1 :(得分:0)

试试看   列表= response.body()。getList()。getWeather();

答案 2 :(得分:0)

     @SerializedName("list")
     List<ListPOJO> listList;

 class ListPOJO{

    @SerializedName("main")
    Main main;

    @SerializedName("wind")
    Wind wind;

    @SerializedName("dt")
    long time;@SerializedName("weather")
List<Weather> weather;

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

public List<ListPOJO> getList() {
    return listList;
}



    public Main getMain() {
        return main;
    }

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

    public Wind getWind() {
        return wind;
    }

    public long getTime() {
        return time;
    }
}

class Main {

    @SerializedName("temp")
    private Double actualTemperature;
    @SerializedName("pressure")
    private Double airPressure;
    @SerializedName("humidity")
    private Double airHumidity;
    @SerializedName("temp_min")
    private Double minTemp;
    @SerializedName("temp_max")
    private Double maxTemp;


    public Double getActualTemperature() {
        return actualTemperature;
    }

    public Double getAirPressure() {
        return airPressure;
    }

    public Double getAirHumidity() {
        return airHumidity;
    }

    public Double getMinTemp() {
        return minTemp;
    }

    public Double getMaxTemp() {
        return maxTemp;
    }

    public Main(Double actualTemperature, Double airPressure, Double airHumidity) {
        this.actualTemperature = actualTemperature;
        this.airPressure = airPressure;
        this.airHumidity = airHumidity;
    //            this.weather.forecastIcon = forecastID;
    }
}

class Wind{
    @SerializedName("speed")
    private Double windSpeed;

    public Double getWindSpeed() {
        return windSpeed;
    }
}


class Weather{
    @SerializedName("icon")
    private String forecastIcon;

    public String getForecastIcon() {
        return forecastIcon;
    }
}

答案 3 :(得分:0)

您的JSON不完整。 使用http://www.jsonschema2pojo.org/进行pojo,然后尝试进行迭代。 谢谢。