使用Java访问JSON数组中的元素

时间:2016-10-23 04:20:19

标签: android arrays json

这里我有json响应的结果,我需要获得正确的代码才能访问预测数组,我该如何实现?

{
 "query": {
  "count": 1,
  "created": "2016-10-23T02:37:21Z",
  "lang": "en-US",
  "results": {
   "channel": {
    "units": {
     "distance": "mi",
     "pressure": "in",
     "speed": "mph",
     "temperature": "F"
    },
    "title": "Yahoo! Weather - Kingston, Saint Andrew, JM",
    "link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-109251/",
    "description": "Yahoo! Weather for Kingston, Saint Andrew, JM",
    "language": "en-us",
    "lastBuildDate": "Sat, 22 Oct 2016 09:37 PM EST",
    "ttl": "60",
    "location": {
     "city": "Kingston",
     "country": "Jamaica",
     "region": " Saint Andrew"
    },
    "wind": {
     "chill": "77",
     "direction": "0",
     "speed": "0"
    },
    "atmosphere": {
     "humidity": "90",
     "pressure": "988.0",
     "rising": "0",
     "visibility": "15.3"
    },
    "astronomy": {
     "sunrise": "6:3 am",
     "sunset": "5:40 pm"
    },
    "image": {
     "title": "Yahoo! Weather",
     "width": "142",
     "height": "18",
     "link": "http://weather.yahoo.com",
     "url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"
    },
    "item": {
     "title": "Conditions for Kingston, Saint Andrew, JM at 09:00 PM EST",
     "lat": "18.015711",
     "long": "-76.79731",
     "link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-109251/",
     "pubDate": "Sat, 22 Oct 2016 09:00 PM EST",
     "condition": {
      "code": "27",
      "date": "Sat, 22 Oct 2016 09:00 PM EST",
      "temp": "77",
      "text": "Mostly Cloudy"
     },
     "forecast": [
      {
       "code": "4",
       "date": "22 Oct 2016",
       "day": "Sat",
       "high": "80",
       "low": "75",
       "text": "Thunderstorms"
      },
      {
       "code": "4",
       "date": "23 Oct 2016",
       "day": "Sun",
       "high": "80",
       "low": "75",
       "text": "Thunderstorms"
      },
      {
       "code": "4",
       "date": "24 Oct 2016",
       "day": "Mon",
       "high": "80",
       "low": "76",
       "text": "Thunderstorms"
      },
      {
       "code": "4",
       "date": "25 Oct 2016",
       "day": "Tue",
       "high": "80",
       "low": "74",
       "text": "Thunderstorms"
      },
      {
       "code": "4",
       "date": "26 Oct 2016",
       "day": "Wed",
       "high": "80",
       "low": "75",
       "text": "Thunderstorms"
      },
      {
       "code": "4",
       "date": "27 Oct 2016",
       "day": "Thu",
       "high": "80",
       "low": "75",
       "text": "Thunderstorms"
      },
      {
       "code": "4",
       "date": "28 Oct 2016",
       "day": "Fri",
       "high": "78",
       "low": "74",
       "text": "Thunderstorms"
      }
     ],

     "guid": {
      "isPermaLink": "false"
     }
    }
   }
  }
 }
}

这就是我的尝试,但它不起作用。我只需要正确访问预测的JSONArray,有正确的表示法吗?

public void populate(JSONObject data) throws JSONException {

JSONArray jArr = data.getJSONObject("item").getJSONObject("condition").getJSONArray("forecast");


for(int i=0;i<jArr.length();i++){
    JSONObject jDayForecast = jArr.getJSONObject(i);
    String date =  jDayForecast.getString("date");
    String text =  jDayForecast.getString("text");
}

}

这行总是返回null,因为它没有引用预测数组?

JSONArray jArr = data.getJSONArray(“forecast”);

2 个答案:

答案 0 :(得分:1)

你得错了对象

JSONArray jArr = data.getJSONObject("query")
.getJSONObject("results")
.getJSONObject("channel")
.getJSONObject("item")
.getJSONArray("forecast");

如果您将channel对象传递给populate函数:

JSONArray jArr = data.JSONObject("item")
.getJSONArray("forecast");

答案 1 :(得分:1)

您可以尝试这种方式:

try{
        /*Your response Object*/
        JSONObject responseObj = new JSONObject(mResponse);

        /*Getting query Object*/
        JSONObject queryObj = responseObj.getJSONObject("query");

        /*Getting result Object*/
        JSONObject resultObj = queryObj.getJSONObject("results");

        /*Getting channel Object*/
        JSONObject channelObj = resultObj.getJSONObject("channel");

        /*Getting item Object*/
        JSONObject itemObj = channelObj.getJSONObject("item");

        /*Getting forecast Array*/
        JSONArray forecastArray = itemObj.getJSONArray("forecast");

        /*Now you could iterate your array*/
        for(int i = 0 ; i < forecastArray.length(); i++){

            JSONObject forecastObj = forecastArray.getJSONObject(i);
            String code = forecastObj.getString("code");
            // As same as other parameters 
        }


    }catch (Exception e){

    }