🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this example, we use the Gson tree model API to read Java objects from JSON file.
Read a JSON file Using GSON in Java
Let's read below students.json file which contains an array of student objects.
src/main/resources/students.json:
[
  {
    "studentId": 100,
    "studentName": "student1"
  },
  {
    "studentId": 200,
    "studentName": "student2"
  },
  {
    "studentId": 300,
    "studentName": "student3"
  },
  {
    "studentId": 400,
    "studentName": "student4"
  },
  {
    "studentId": 500,
    "studentName": "student5"
  }
]
Now, let's read above students.json file into Java objects and print to console:
package net.guides;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class GsonTreeModelRead {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        String fileName = "src/main/resources/students.json";
        Path path = Paths.get(fileName);
        try (Reader reader = Files.newBufferedReader(path,
            StandardCharsets.UTF_8)) {
            JsonParser parser = new JsonParser();
            JsonElement tree = parser.parse(reader);
            JsonArray array = tree.getAsJsonArray();
            for (JsonElement element: array) {
                if (element.isJsonObject()) {
                    JsonObject car = element.getAsJsonObject();
                    System.out.println("********************");
                    System.out.println(car.get("studentId").getAsLong());
                    System.out.println(car.get("studentName").getAsString());
                }
            }
        }
    }
}
Output:
********************
100
student1
********************
200
student2
********************
300
student3
********************
400
student4
********************
500
student5
In the example, we read JSON data from a file into a tree of JsonElements.
JsonParser parser = new JsonParser();
JsonElement tree = parser.parse(reader);
JsonParser parses JSON into a tree structure of JsonElements.
JsonArray array = tree.getAsJsonArray();
We get the tree as JsonArray.
 for (JsonElement element: array) {
                if (element.isJsonObject()) {
                    JsonObject car = element.getAsJsonObject();
                    System.out.println("********************");
                    System.out.println(car.get("studentId").getAsLong());
                    System.out.println(car.get("studentName").getAsString());
                }
}
We go through the JsonArray and print the contents of its elements.
 
 
 
![[NEW] Full-Stack Java Development with Spring Boot 3 & React Build 5 Spring Boot Projects with Java: Line-by-Line Coding](https://img-c.udemycdn.com/course/750x422/5338984_4d3a_5.jpg) 
 
 
 
 
 
 
 
 
 
 
Comments
Post a Comment
Leave Comment