Basic Tutorial: Master the skills of processing multi-dimensional JSON arrays in Java
Introduction:
In modern software development, with the popularity of front-end and back-end separation, JSON (JavaScript Object Notation) has become a common standard for data transmission and storage. In the actual development process, we often encounter the processing of multi-dimensional JSON arrays. This article will introduce several techniques for processing multi-dimensional JSON arrays in Java and provide corresponding code examples.
import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject;
{ "students": [ { "name": "张三", "age": 18, "courses": [ { "name": "数学", "score": 90 }, { "name": "语文", "score": 85 } ] }, { "name": "李四", "age": 20, "courses": [ { "name": "英语", "score": 95 }, { "name": "物理", "score": 88 } ] } ] }
We can parse it into a JsonObject object in Java through the Gson library:
String json = "{...}"; // 假设这里是上述JSON数据 Gson gson = new Gson(); JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
JsonArray students = jsonObject.getAsJsonArray("students"); for (JsonElement studentElement : students) { JsonObject studentObj = studentElement.getAsJsonObject(); String name = studentObj.get("name").getAsString(); int age = studentObj.get("age").getAsInt(); System.out.println("姓名:" + name + ",年龄:" + age); }
JsonArray students = jsonObject.getAsJsonArray("students"); for (JsonElement studentElement : students) { JsonObject studentObj = studentElement.getAsJsonObject(); String name = studentObj.get("name").getAsString(); JsonArray courses = studentObj.getAsJsonArray("courses"); for (JsonElement courseElement : courses) { JsonObject courseObj = courseElement.getAsJsonObject(); String courseName = courseObj.get("name").getAsString(); int score = courseObj.get("score").getAsInt(); System.out.println("姓名:" + name + ",课程:" + courseName + ",成绩:" + score); } }
class Student { private String name; private int age; private List<Course> courses; // getter和setter方法 } class Course { private String name; private int score; // getter和setter方法 }
Then, we can convert the multidimensional JSON array into a Java object through the following code:
JsonArray students = jsonObject.getAsJsonArray("students"); List<Student> studentList = new ArrayList<>(); for (JsonElement studentElement : students) { JsonObject studentObj = studentElement.getAsJsonObject(); String name = studentObj.get("name").getAsString(); int age = studentObj.get("age").getAsInt(); JsonArray courses = studentObj.getAsJsonArray("courses"); List<Course> courseList = new ArrayList<>(); for (JsonElement courseElement : courses) { JsonObject courseObj = courseElement.getAsJsonObject(); String courseName = courseObj.get("name").getAsString(); int score = courseObj.get("score").getAsInt(); Course course = new Course(); course.setName(courseName); course.setScore(score); courseList.add(course); } Student student = new Student(); student.setName(name); student.setAge(age); student.setCourses(courseList); studentList.add(student); }
Summary:
Introduction to this article Learn the techniques for handling multi-dimensional JSON arrays in Java, including parsing, retrieving data, processing nested data, and converting to Java objects. By mastering these skills, we can handle multi-dimensional JSON arrays more flexibly and improve development efficiency. I hope this article will be helpful to readers in actual development work.
The above is the detailed content of Basic tutorial: Master the skills of processing multi-dimensional JSON arrays in Java.. For more information, please follow other related articles on the PHP Chinese website!