【问题标题】:How to access json array (Android)如何访问 json 数组(Android)
【发布时间】:2018-11-16 01:21:47
【问题描述】:

我正在使用 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;
    }
}

【问题讨论】:

  • 请勿使用代码图片
  • 尝试将您的代码从屏幕截图转换为文本格式。
  • 你可以尝试在你的 pojo 类中添加序列化名称
  • 检查this answer是否可以帮助您。
  • 我在 json 结构中没有看到 forecastList 属性

标签: android json weather openweathermap


【解决方案1】:

试试看

@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

}

【讨论】:

  • 1 类用于根对象,1 类用于预测项。为什么你有3类预测输出数据,预测列表,预测项?
  • 添加后,该行将显示红色下划线并显示“ForecastData.List 没有类型参数”
  • 它是 Java.util.List
  • ForecastOutputData 指的是集合中的主要元素,分别是“cod”、“message”、“cnt”、“list”和“city”。 ForecastList 指的是 json 数组中称为 list 的元素,包括“dt”、“main”、“weather”、“clouds”等。
猜你喜欢
  • 2019-11-16
  • 1970-01-01
  • 2014-11-01
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 2020-12-26
  • 2015-06-26
相关资源
最近更新 更多