Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Introducing JSONObject7:27 with Ken Alger
Android has some built-in objects for parsing JSON data that make it very easy to work with the JSON format. The basic object we will use is called JSONObject. Let's see how to parse the key weather data we need using this object.
Android provides some convenient ways to handle JSON data. 0:00 Making it easy to work with. 0:04 With our app connected to and receiving weather data in a JSON format. 0:06 Let's take a look at how we can leverage the tools provided by Android to parse 0:10 the weather data to get the information our app will use. 0:14 We have our new data model object, CurrentWeather. 0:18 Let's put it to use in main activity. 0:21 So under our tag here, let's create a new, private, 0:25 CurrentWeather object, and we'll call it currentWeather. 0:29 Now, we'll want to set this, once we have a successful response. 0:34 So we scroll down to our onResponse, and 0:38 inside our if statement here where our response is successful. 0:41 Let's set currentWeather. 0:46 And we'll set it to a new method that we'll define here in a moment. 0:49 We'll call it getCurrentDetails. 0:55 We'll want to pass in JSON data. 0:58 We can use the response.body that we have up here in our tag. 1:01 But let's actually refactor that into a variable, and use it in both places. 1:12 String.jsonData. 1:20 Now we can pass in jsonData. 1:26 Okay, now let's use the quick fix provided by Android Studio we put there. 1:42 Alt + Enter, Create method. 1:47 And again we want that inactivity not in our onCreate anonymous callback. 1:51 Next, we're going to work with a special class called JSONObject. 2:01 This class is able to hold any object represented in JSON format, and 2:05 provides a few different methods to access the properties inside the JSON data. 2:10 The JSONObject class has a constructor that lets us pass in a string 2:15 of jsonData to create the new JSONObject. 2:19 Fortunately, that's just what we have as a parameter. 2:23 Let's create a new JSONObject inside our new method. 2:27 JSONObject, plot forecast cuz that's what we're getting back. 2:31 We see that we have an error here. 2:41 We hover over it. 2:43 We see that it is an unhandled exception. 2:44 This is similar to the IOException we previously seen. 2:48 But this is a JSONException. 2:51 Just like we've done before, we can use the quick fix to add a dry catch. 2:54 Now, while this works, this isn't ideal. 2:59 It would be better if we could have the exception handle, or 3:03 the getCurrentDetails method is called. 3:05 Up here where the IOException is being caught. 3:10 It would be great if we could get this new exception to bubble up, and 3:13 be caught up here. 3:16 We can do that with a throws keyword. 3:18 Back here in getCurrentDetails, let's undo our try/catch block, 3:20 and after our method signature before the curly brace, we wanna type throws. 3:27 And then we need the specific type of exception. 3:34 In this case, it is a JSONException This 3:37 moves the responsibility of exception handling from this line here in 3:42 getCurrentDetails to up here where the method is called. 3:47 We see now in onResponse where getCurrentDetails is called, 3:52 it has an error. 3:56 Since we're already inside a try block, we can catch the JSONException down here. 3:57 We'll catch the JSONException. 4:03 And we'll add a log statement for it. 4:13 Now our code is a bit more organized since our exceptions are all being handled in 4:23 one place. 4:28 With that exception handled, we can continue to set our JSON object. 4:29 Let's take a quick look at the Android documentation on JSON object. 4:33 Let's see what types of data we can get. 4:42 We see that this class has several methods that allow to get different types of data. 4:46 Strings, ints, booleans, JSON arrays. 4:57 Even other JSON objects. 5:01 We'll see some of these methods used in other projects, as well. 5:03 Let's take another look at our data as a guide for 5:07 how we should parse our JSON object. 5:09 The JSON object forecast that we just created in our code 5:11 corresponds to this root object up here. 5:16 Remember our root object starts with a first opening brace. 5:21 We're passing latitude and longitude in as parameters to the API call. 5:25 So we don't need to worry about those. 5:29 Let's use this timezone field as an example to start. 5:31 timezone in all lower case is the key we want to get. 5:35 The value is enclosed in double quotes. 5:39 That tells us that it is a string value. 5:42 Let's go back to our getCurrentDetails method. 5:46 So we can access this timezone, and write it to the log. 5:52 So String_ timezone. 5:57 We're gonna get forecast.getString. 6:02 We need to pass in the key we want. 6:07 We say that it was timezone in all lower case. 6:09 Then we want to log that. 6:16 So Log.i, TAG say From JSON 6:20 plus our timezone. 6:26 Cool, we should test this out just to make sure we are, 6:32 in fact, getting what we're expecting. 6:36 This is important, especially when trying to parse through complex data. 6:38 Building things one step at a time, and 6:42 verifying them along the way is a great habit to get into. 6:44 Otherwise, you can end up with a lot of code that can be broken in multiple 6:48 places. 6:52 Now, before we run it, 6:53 we have another area here because we aren't returning anything. 6:55 Let's just return null for now. 6:58 Run our app. 7:06 And checking here in the Logcat. 7:12 Here's our parsed data from JSON, America/Los_Angeles. 7:17 It's working! 7:20 That's great! 7:21 Let's take a quick break, and 7:22 we'll get our current weather all set up when we come back. 7:23
You need to sign up for Treehouse in order to download course files.Sign up