0

I am trying to get the information within the DATA array, but keep getting nothing. I am parsing this data from 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
}..................}

I would like to loop through the data array.

I get the Hourly data by doing this:

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

However, I am not getting the data array from within the hourly string. What am I missing?

Am I on the right path here:

   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 Answers 4

2

Your Hourly is a JSONObject and inside it a JSONArray named data by using org.json API you can easily loop throw them using the following code:

   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 
   }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I was missing the obj within my array initally. This helped!
2

You need to use JSONArray, as you've mentioned it yourself, that data is an array in your JSONObject.

JSONArray dataArray = hourlyObj.getJSONArray("data");

Comments

1

Use JSONArray to get the data value and JSONObject to get hourly.

Hourly Object

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

Data Array

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 
     }

You will get all the values in data.

2 Comments

Great answer. Well laid out :) Thanks for adding to my question.
Glad to help you.If my answer helped you. then upvote and mark as an answer.
0

Here is a sample snippet that would help you.

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);

                }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.