From the course: JSON Processing with Java EE

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Create a model from a JSON file

Create a model from a JSON file

From the course: JSON Processing with Java EE

Start my 1-month free trial

Create a model from a JSON file

- [Instructor] Okay, so go ahead and open up the ObjectExample2 class. In the loadJasonObject() method, we're going to start to write the code in the same way that we did in the previous example, but instead of creating a StringReader, we create a FileReader. So let's start as we did before. We need a JsonReader, and we need to create a factory. We do this by calling a static method on the Json class. In this case, we're going to pass it a new FileReader, and we're going to pass it our jsonFileObject. Now, at the top of the screen, you can see that we have our jsondata-object.json file. This file is located in the resources directory. What the FileReader will do is to open this file and load in the JSON data. Just as we did before, what we need to do is to call the readObject method on the reader instance. So we're going to create our jsonObject instance. We're going to be calling our readObject method, which reads the object, into our jsonObject. Just as before, we need to return our…

Contents