Join Alex Theedom for an in-depth discussion in this video Traverse the JSON model, part of JSON Processing with Java EE.
- [Instructor] So, let's get started with the JSON model. I'm gonna scroll down to the bottom of this class, and what I have here is a method called load JSON object. And what this method does is it creates a JSON object. This code might look familiar to you. It is actually a copy of the code we used in an earlier lesson. When I call the load JSON object method, it returns a JSON object that represents the JSON data that we've been using in this course. So the objective here is to write code that traverses the object model and prints out the model's data to the console.
So let's get started. What I've done here is I've created a main method, and within the main method, I call a method called navigate JSON structure, and I pass the navigate JSON structure, the JSON object. And within the navigate JSON structure, we have a switch statement. And the switch statement retrieves the value type from the JSON value, and with that value type, it selects an action to perform. So if the value type is object, it performs the code related to the object case. Now what we're going to do in this lesson is to go through and to fill out the code for each of these different cases.
The first case we're going to deal with is the number case. This is the simplest of all cases. And all we need to do here is to select the value and print it out to the screen. We have a nice convenience method called print value, and I'm gonna pass this method, JSON value. I'm gonna select two string, and that's it. All we need to do in order to print out the number value is to call the two-string method on the JSON value instance. Okay, let's move on and have a look at the string case. So the string case is very similar.
But all I have to do here is to cast the JSON value to a JSON string. So let's do that. What I need to do here is to take my JSON value, as before, and cast it to a JSON string. And this is a safe cast, because we already know that our JSON value is of type JSON string. I then call the get string method, and with that, I have a string value.
And all I need to do now is to pass it through to my print value method, which prints it to the console. And there we have it. Pretty simple case. Now, let's write a slightly more complicated example. This one requires some recursion logic. So let's have a look at the array type. The array type contains elements that could be other complex types, such as objects and arrays. And we need a way to deal with this situation. And the way we deal with it is by using recursion.
So let's write the array case. So first of all, we need to iterate over the array. So we use a symbol for each statement. So for each JSON value, in our JSON array, and again, what we do, is we cast our JSON value to a JSON array. This is a safe cast because we already know what type it is. And then we simply delegate responsibility of dealing with each of those elements to our navigate JSON structure.
And that's it. This is how we use recursion to deal with the elements within a JSON array. Let's move on to a more complex example, the object type. So what I need to do for the object type is to get a set of the keys, and iterate over that set, and call the navigate method for each one of those values. So let's do that. So the first thing we need to do is we need to cast the JSON value to a JSON object. And we know we're gonna get a JSON object, so we have a JSON object, and then we create a cast of the JSON object, like this, okay? And now we need to iterate over the key set.
Now remember that the JSON object extends the map interface, and because of that, we can actually just simply call key set on the JSON object. So we create a for each loop, we're getting a set of keys, which have a name. And then for the JSON object, we just call key set. And then what we do, as we did before, is we call our navigate JSON structure method, we pass it, the actual key value, and we do this by calling the get method on the JSON object and passing it the key name.
And then we pass the name of the key, as well. So what we've done here is to use recursion by calling the navigate JSON structure on each of the elements within our JSON object. And each of those elements could be a string or an array or another object. So we're using recursion here to deal with the complex situation of nested objects. Now that we've finished writing the code, let's execute it and see what the output looks like in the console window. Let's expand this a little bit. And there we have it. As you can see, this is our JSON structure.
We have our title, we have how title value, have chapter, and within that we have each of the values of the chapter with a conclusion. We have the release, source code, and so on and so forth. This should be very familiar to you because this is exactly the same JSON structure that we used earlier on in the course. This is not the only way to tackle this task. Let's take a look at how to use polymorphism to traverse the JSON object. Okay, let's load up class object example five. And as you can see, in this example, I have used the JSON value's polymorphic type to select the way to treat each type of JSON value.
Here in the print value method, you can see that I'm filtering on the four main types. And based upon the type, I'm stating one of the four print value methods. So if the type is JSON array, JSON object, JSON number, or JSON string, a different print value method will be selected. And just as before, the JSON object and JSON array types are iterated and each element is treated based on its type. Why don't you spend some time reviewing this code, in order to understand how it works?
In this course, discover how to use the Java Enterprise Edition (EE) JSON-Processing API. With its two programming models, JSON-Processing is one of the simplest ways to generate, query, and parse JSON data, and you'll find that implementing it in your projects is quick and easy. Join Alex Theedom as he covers an array of topics, including how to use both the object model and the streaming model, how to traverse a JSON structure, and how to parse JSON data using the extensive builder APIs. When you wrap up this course, you'll be ready to develop with this essential and powerful API.
- Working with JSON and Java
- JSON-Processing API features
- The Java EE JSON object model
- Creating a model from a JSON string
- Building a JSON model
- Traversing the JSON model
- Outputting the JSON model to a data stream
- Parsing and writing JSON data