Join Alex Theedom for an in-depth discussion in this video Parse JSON data, part of JSON Processing with Java EE.
- [Instructor] Okay, let's get started by writing code that passes the json document that we have been using throughout this course. Okay, so let's open the streaming example one class from the exercise files. And as you can see, I have already created a method called pause json string. This is where we are going to write the pausing logic. I also assigned the json data into a string variable called json. Ready for us to stream. Firstly, we create the passer from the static factory method in the json class. And pass it an instance of the reader object.
So, let's do that. So as before, we start without json passer. Like so. And then from the json class, we call a factory method. And from that factory method we pass in a string reader. And into that string reader, we pass the string that we want to pass just as we had done before. In this case, a simple string reader instance is sufficient for our purposes.
Nevertheless, you could just as easily pass it an input stream instead. Now we have a pasted instance configured with the json data. We need to write code that iterates over the instances events. We can use a simple while lib and classic iteration semantics. We can start with while, and we go into call the has next method. And we're going to iterate while the passer has the next event. And then we need to actually get the next event.
We do this by quite simply calling the next method. And what this does, it gets the next event in the sequence of events. So here what we're doing is we're testing to see if there's another event. And then we are calling the next method to get the actual event at that stage of the iteration. So, given the event object, you can implement a switch-case structure to select the action to perform for each of those types. So let's implement our switch. We're gonna pass our switch the event and now what we're going to do is to start constructing case statements for each of the events.
So, again, we create a case for starter eight. We're gonna create another case for the end of an array. I'm gonna create another for start of object. And we just gonna continue 'til we've created a case for each of the events that we saw detailed in the previous video. So the next one we're gonna do is gonna be the end of the object.
Again, another one for values. So now we're gonna go for the list of values we've done. The start and end of an array and object. And then we're gonna go through the value events. So the first value we're gonna do is for the real value of false. Next one we're going to do a case statement for true. And then next will be for key name and my ID is helping me a lot here by auto-suggesting what the cases should be.
So the next one is going to be a value string. And the next one is going to be number. Or actually, up here, what I'm going to do is I'm gonna put in the final one. Which is going to be null. Okay. Now, what we have here is a case statement for each of the events within the json passer dot event enum. And we want to do something useful with these cases. What we want to do, is to print out to the console the event and the value of that event.
Now, as you know, the start array end array, start object, and end object, they don't have any values. The value false, value null, and value true don't either. They can be printed out directly as two strings. So, what we're going to do in the true, is we're going to start to just simply do a system dot out dot print line. And we're gonna print out by simply event dot two string.
So I'm just gonna print out the name of this event. Also, don't forget to put in a break statement here. Now we haven't put in a break statement in the previous case statements. That's because we want it to fall through to this particular statement here and just print out the event as a string value. The next is the key name and here again, we would like to do the same thing. And here, we're going to print out the event. We're gonna do two string.
What's interesting here is not only just to print out the event but also to print out the actual key name itself. And we do that by accessing the passer, and calling the get string value on that. Okay? And again, what we need to do is to finish with a break statement. And for the final two, we're gonna do very much the same thing. What you can see here is I have written a case statement for each of the event types and used the for through feature of k statements devoid to repetition.
So let's execute this and have a look at the output. Okay, let's make this slightly bigger. And here, we can see it prints out the event, start object. Then it prints out the key name and then title. It prints out the value of the title which is json processing with Java E and moves onto the next event, which is key name. And the key name is chapter. Then we have the start event, which is the start array. And then value for each of the values there.
And then the end of the array and then we switch onto key. And so on and so forth in through the entire structure of the json string. And firing an event for each of the elements that we can see here. Free to the start and end events and the value and key events until we reach the very end where it fires the end uptake event. Okay, so as you can see, all this does is it prints out the events fired by the passer and the value, if any, associated with that event.
So, now, of course this is just a demonstration and not very practical. The streaming model comes into its own when it is used to retrieve a specific value from within the stream of json data. And this will form your next challenge.
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