Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Setting CurrentWeather from JSON5:14 with Ken Alger
Now it's time to actually populate our data model object with the real weather data from Dark Sky.
We've seen how to access our JSON weather data and parse single field data. 0:00 Next, let's look at how we can drill down and get data inside a nested JSON object. 0:05 For our Stormy app, we want to get data inside the currently key, seen here. 0:11 Why don't you give it a try yourself? 0:17 Pause me, write it out, and come back and see how you did. 0:20 Let's declare a new JSONObject variable. 0:27 So we can put it down here. 0:30 We'll call it currently. 0:35 It's equal to forecast. 0:39 We'll getJSONObject ("currently"). 0:44 How did you do? 0:50 I'm sure you did great. 0:53 Let's take another look at our JSON. 0:54 We now have all the data we need to create a new CurrentWeather object using 1:02 our CurrentWeather model. 1:06 There are several data points to fill in here, so let's get started. 1:12 Go back over here to MainActivity. 1:17 Let's create our CurrentWeather object. 1:20 CurrentWeather. 1:24 Call it currentWeather. 1:25 New CurrentWeather object. 1:27 Now we set all the values from our data model. 1:31 SetHumidity. 1:38 It's a double, so we getDouble. 1:42 And it's humidity. 1:48 SetTime. 1:56 GetLong, and that's time. 2:03 Set our Icon. 2:11 For the location label, we'll just hard code the location, Alcatraz Island, 2:22 California. 2:26 CurrentWeather, SetLocationLabel, 2:29 Alcatraz Island, California. 2:38 Oops. 2:48 CurrentWeather.setPrecipChance. 2:51 And that's a double, and that key was precipProbability. 3:00 Set the Summary. 3:15 GetString("summary"). 3:20 And then we'll set the Temperature. 3:24 And that's a double, and that's temperature. 3:37 Now that we have a CurrentWeather object, 3:43 we can return that instead of returning null. 3:45 Great work! 3:52 How do we check that everything is working so far, though? 3:55 We don't have a layout yet to view all this data. 3:58 We could add a lot of log statements to our code, but that seems tedious. 4:01 Let's use the debugger. 4:06 If we set a break point in our code, 4:08 we can investigate the variables at that break point. 4:10 So let's set a break point here at CurrentWeather. 4:13 And run the app, using the debugger. 4:16 Go here into the Debug screen Show our currently object, 4:24 Step into it, So right now, we don't have any values anywhere. 4:36 We step over each line. 4:44 If we open up CurrentWeather, we notice we don't have any values. 4:48 If we step over, Everything will get populated line by line. 4:53 Congratulations! 5:08 We now have a fully populated data model. 5:10 Nice work. 5:13
You need to sign up for Treehouse in order to download course files.Sign up