Join Alex Theedom for an in-depth discussion in this video How to traverse the model, part of JSON Processing with Java EE.
- [Instructor] Once a model has been constructed, we can navigate the model and inspect its keys and values. In this video, we're going to look at two ways of traversing the JSON model. Remember that in an earlier video, we talked about how all types within the JSON model extend from JsonValue. Well, we're going to use this knowledge to construct a method that traverses the JSON tree and extracts its values. We're going to implement this in two contrasting ways. For the first method, we're going to use the ValueType to determine how to treat the JsonValue object. And for the second, we're going to use polymorphism to select the right method to deal with a JsonValue object.
Let's have a closer look at the methodology we're going to use to construct the first traversal code. We're going to use the ValueType information that we can extract from the JsonValue instants. We know that by calling the get value type method, we can determine the internal type represented by this object with one of the following. Array, object, string, number, true or false, or null. We saw this in an earlier lesson. With this information, we can construct a series of conditional statements that process a ValueType based on knowing the data its subtype represents.
So, for example, if we discover that the internal type is a string, we know that no further processing of this type is necessary. We can just use it as is. Perhaps to the console. If the type is an array, we know that we have to iterate over the array and process each element. While doing so, we must bear in mind that each element of that array could be one of those seven value types. It is here that we need to use recursion to resolve issues arising from nested types such as arrays and objects. Okay, so let's jump into the IDE and get started.
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