从Android Studio中的JSON数组获取值

时间:2015-12-08 13:00:43

标签: java android json

我想从预测中获得第一个“低”( - 5)的值,但我得到了空错误。我做了一些研究,但我找不到答案。例子很复杂。

 "item": {

     "forecast": [
      {
       "date": "7 Dec 2015",
       "day": "Mon",
       "high": "1",
       "low": "-5",
      },
      {
       "date": "8 Dec 2015",
       "day": "Tue",
       "high": "-2",
       "low": "-6",
      },

     ],

Item.java

private Forecast forecast;

    public Forecast getForecast() {
        return forecast;
    }
forecast=new Forecast();
        forecast.paupulate(data.optJSONObject("forecast[0]"));

Forecast.java

private int low;
    @Override
    public void paupulate(JSONObject data) {
        low=data.optInt("low");
    }
    public int GetLow(){
             return low;
        }

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你的Pojo将预测json视为一个对象,但它是一个数组

您需要将您的pojo更改为 -

private Forecast[] forecast;

public Forecast[] getForecast() {
    return forecast;
}

然后为了获得第一个低(-5)你做

Forecast[] forecasts = myItem.getForecast();
int requiredValue = forecasts[0].GetLow();