Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
JSONArray allows for working with more complex JSON data than JSONObject in Android.
- JsonArray Documentation
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
So far, we've been using the JSON object
class to work with our JSON data.
0:00
Now, we're going to look at another
important class we're working with JSON in
0:04
Android development, JSON array.
0:08
Recall from previous explorations that
JSON data is stored in two formats.
0:11
A JSON object,
which is a collection of key-value pairs.
0:16
And an ordered-list of JSON objects,
which we can think of as an array.
0:20
JSON object takes care of regular objects.
0:26
An JSON array is basically an array
that can hold JSON objects,
0:30
or even other JSON arrays.
0:35
Recall that arrays in JSON
are indicated with square brackets.
0:38
Here in the hourly session,
we have an array called data.
0:42
Each JSON object inside the array
is surrounded with curly braces.
0:48
We'll access this data array in code and
then store object
0:58
in here as an array item
using our new hour class.
1:04
Lets go back to Android Studio.
1:10
Let's take another look at our
new parseForecastData method.
1:14
Our Forecast class has that array to fill,
an hourly forecast.
1:18
Let's create a new method to fill it
following the format we used to fill
1:22
the current weather data.
1:26
We can write the code we want and then
autogenerate the new method declaration.
1:28
Inside parseForecastData,
we can add our new statement here.
1:32
We want forecast.setHourlyForecast, and
1:40
our new method we'll
call getHourlyForecast.
1:45
And we'll pass in jsonData as well.
1:51
And we can use the quick
fix to generate our method.
1:54
So inside getHourlyForecast,
2:01
we want to start just like
we do in getCurrentDetails.
2:03
We'll get a JSONObject.
2:08
We'll call it forecast.
2:11
And set new JSONObject from our jsonData.
2:16
We want to grab the timezone, just like
we do in getCurrentDetails, as well.
2:22
Timezone.
2:29
It's gonna be forecast.getString.
2:33
And the timezone was
timezone in all lower case
2:38
And we have that unhandled
JSON exception error again.
2:45
We can do the quick fix to add
the exception to our method signature.
2:49
All right, now how is that
JSON data structured again?
2:55
Let's take another look.
3:00
Hm, it looks like we need this
hourly JSON object from the data.
3:06
Okay, we can do that.
3:12
Let's create a new JSON object,
we'll call it hourly.
3:17
And then set it from the forecast
object using the getJSONObject method.
3:20
JSONObject hourly forecast.getJSONObject.
3:27
We just saw in the data that
key we need is called hourly,
3:35
in all lower case, hourly.
3:39
Now we're ready to drill down and
get our data array.
3:45
Let's take one more look at our JSON data,
just to make sure.
3:48
The hourly object has an array named data.
3:55
We can use this name as the key to
get the array, just like we did for
3:59
the hourly object.
4:03
JSONArray, We'll call it data.
4:08
Hourly.getJSONArray.
4:15
And it's data.
4:19
Now we have an array of JSONObjects,
great work!
4:23
We still want an array of our objects,
though.
4:26
How should we go about converting them?
4:30
Why don't you give it a try yourself and
4:32
then come back in the next
video to see how I tackled it.
4:34
Here's a hint,
4:38
you can access the length of JSONArray
object by using a method called length.
4:39
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up