0

我正在尝试获取 DATA 数组中的信息,但一无所获。我正在从 Dark Sky API 解析这些数据:

hourly: {
summary: "Mostly cloudy later this morning.",
icon: "partly-cloudy-day",
data: [
{
time: 1364389200,
summary: "Partly Cloudy",
icon: "partly-cloudy-night",
precipIntensity: 0,
temperature: 29.8,
windSpeed: 2.44,
windBearing: 60,
cloudCover: 0.41,
humidity: 0.88,
pressure: 831.54,
visibility: 6.15
},
{
time: 1364392800,
summary: "Partly Cloudy",
icon: "partly-cloudy-day",
precipIntensity: 0,
temperature: 29.26,
windSpeed: 2.95,
windBearing: 45,
cloudCover: 0.36,
humidity: 0.88,
pressure: 832.05,
visibility: 6.14
}..................}

我想循环遍历数据数组。

我通过这样做获得每小时数据:

String hourly = json.getString("hourly");

但是,我没有从每小时字符串中获取数据数组。我错过了什么?

我在正确的道路上吗:

   JSONObject h = json.getJSONObject("hourly");
    String d = json.getString("data");

JSONArray a = h.getJSONArray("data");

                for(int i = 0; i < a.length(); i++){
                    // Pulling items from the array
                    String s = h.getString("summary");
                    String t = h.getString("temperature");
                    aq.id(R.id.tv).text("Summary: " + s + "Temp: " + t);
                }
4

4 回答 4

2

JSONArray正如您自己提到的那样,您需要使用该数据array在您的JSONObject.

JSONArray dataArray = hourlyObj.getJSONArray("data");
于 2013-03-27T14:29:28.343 回答
2

Hourly是一个 JSONObject 并且在其中JSONArray使用 org.json API 命名数据,您可以使用以下代码轻松循环抛出它们:

   JSONObject hourly = new JSONObject(json.getString("hourly"));
   JSONArray array = hourly.getJSONArray("data");
   for (int i=0; i< array.length(); i++){
       JSONObject obj = data.getJSONObject(i);
         //do whatever you want with your data 
   }
于 2013-03-27T14:34:16.250 回答
1

用于JSONArray获取data值和JSONObject获取hourly.

每小时对象

JSONObject hourlyObj = json.getJSONObject("hourly");

数据数组

JSONArray yourdataArray = hourlyObj.getJSONArray("data"); 
// loop it
 for(int i=0;i<jsonArray.length();i++){  
         JSONObject json_data = jsonArray.getJSONObject(i);
         String time = json_data.getString("time");
         // and so on 
     }

您将获得数据中的所有值。

于 2013-03-27T14:32:45.713 回答
0

这是一个可以帮助您的示例片段。

JSONObject jObj = new JSONObject(results);
                JSONArray albums = jObj.getJSONArray("pickups");

                for (int i = 0; i < albums.length(); i++) {
                    JSONObject c = albums.getJSONObject(i);
                    String  id = c.getString("id");

                    Record objRecord=new Record();
                    objRecord.setId(c.getString("id"));
                    objRecord.setName(c.getString("name"));
                    objRecord.setNumber(c.getString("number"));
                    objRecord.setTime(c.getString("time"));

                    PickupList.arrayList.add(objRecord);

                }
于 2013-03-27T14:36:51.403 回答