如何访问json数组(Android)

时间:2018-11-16 01:21:48

标签: android json weather openweathermap

我正在使用openweathermap api创建一个天气应用程序,并且需要访问一个名为“ list”的json数组的元素。它包含未来几天的天气预报。我也在使用翻新来解析所有json数据。问题是我根本无法访问该阵列。但是,我可以访问城市的元素。

json structure

main class

forecast output data

forecast list

forecast item

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://api.openweathermap.org")
            .addConverterFactory(JacksonConverterFactory.create())
            .build();
    final WeatherService service = retrofit.create(WeatherService.class);

    final WeatherForecast forecast = retrofit.create(WeatherForecast.class);

    sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Call<ForecastOutputData> callForecastRequest = forecast.getWeatherInfo(Double.toString(lat), Double.toString(lon), "imperial", "5", "key");

            callForecastRequest.enqueue(new Callback<ForecastOutputData>() {
                @Override
                public void onResponse(Call<ForecastOutputData> forecastCall, Response<ForecastOutputData> forecastResponse) {
                    ForecastOutputData forecastData = forecastResponse.body();
                    Log.d("GHEY",forecastData.getCity().getName());
                    Test.setText("Test " + forecastData.getList);
                }

                @Override
                public void onFailure(Call<ForecastOutputData> call, Throwable t) {
                    Log.e("mytag", "mymessage",t);
                    Test.setText("Test FAIL");
                }
            });

        }
    });
}

    @JsonIgnoreProperties(ignoreUnknown = true)
public class ForecastOutputData {
    private int cnt;
    private String cod;
    private double message;
    private List<ForecastList> forecastData;
    private City city;

    public String getCod() {
        return cod;
    }

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

    public int getCnt() {
        return cnt;
    }

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

    public List<ForecastList> getForecastData() {
        return forecastData;
    }

    public void setForecastData(List<ForecastList> forecastData) {
        this.forecastData = forecastData;
    }

    public City getCity() {
        return city;
    }

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

    public double getMessage() {
        return message;
    }

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

import java.util.List;

class ForecastList {
    private int dt;
    private Main main;
    private List<Weather> weather;
    private Clouds clouds;
    private Wind wind;
    private Sys sys;
    private String dtTxt;

    public int getDt() {
        return dt;
    }

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

    public Main getMain() {
        return main;
    }

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

    public Clouds getClouds() {
        return clouds;
    }

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

    public Wind getWind() {
        return wind;
    }

    public void setWind(Wind wind) {
        this.wind = wind;
    }

    public Sys getSys() {
        return sys;
    }

    public void setSys(Sys sys) {
        this.sys = sys;
    }

    public String getDtTxt() {
        return dtTxt;
    }

    public void setDtTxt(String dtTxt) {
        this.dtTxt = dtTxt;
    }

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

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

    public class ForecastItem {
    private String cod;
    private double message;
    private int cnt;
    private List<ForecastList> list;
    private City city;

    public String getCod() {
        return cod;
    }

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

    public double getMessage() {
        return message;
    }

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

    public int getCnt() {
        return cnt;
    }

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

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

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

    public City getCity() {
        return city;
    }

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

1 个答案:

答案 0 :(得分:0)

尝试

@JsonIgnoreProperties(ignoreUnknown = true)
class ForecastResponse {
    private String cod;
    private String message;
    private City city;
    @JsonProperty("list")
    private List<ForecastItem> list;
}

public class ForecastItem {
  private int dt;
  //dt, main, weather, clouds etc

}