By GokiSoft.com| 22:37 29/10/2021|
Java Advanced

[Video] Json/Gson & Java - Phân tích dữ liệu lớp học JSON bằng Java -Ứng dụng quản lý lớp học Java nâng cao



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession4;

/**
 *
 * @author Diep.Tran
 */
public class Teacher {
    String fullname;
    int age;
    String address;

    public Teacher() {
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Teacher{" + "fullname=" + fullname + ", age=" + age + ", address=" + address + '}';
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession4;

/**
 *
 * @author Diep.Tran
 */
public class Student {
    String fullname;
    int age;
    String address;

    public Student() {
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Student{" + "fullname=" + fullname + ", age=" + age + ", address=" + address + '}';
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession4;

import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Diep.Tran
 */
public class ClassRoom {
    //dat ten bien trung key trong chuoi json
    String classname;
    String address;
    Teacher teacher;
    
    @SerializedName("student_list")
    List<Student> studentList;

    public ClassRoom() {
        teacher = new Teacher();
        studentList = new ArrayList<>();
    }

    public String getClassname() {
        return classname;
    }

    public void setClassname(String classname) {
        this.classname = classname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }
    
    public void display() {
        System.out.println("Class Name: " + classname + ", Address: " + address);
        System.out.println(teacher);
        
        for (Student student : studentList) {
            System.out.println(student);
        }
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lession4;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        FileReader reader = null;
        
        try {
            //gson java example => vi du trong tung truong hop 1
            reader = new FileReader("student.json");
            
            Type classOfT = new TypeToken<ArrayList<ClassRoom>>(){}.getType();
            
            Gson gson = new Gson();
            ArrayList<ClassRoom> list = gson.fromJson(reader, classOfT);
            
            for (ClassRoom classRoom : list) {
                classRoom.display();
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(reader != null) {
                try {
                    reader.close();
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}



[
	{
		"classname": "T1907A",
		"address": "8 Ton That Thuyet, Cau Giay, HN",
		"teacher": {
			"fullname": "Tran Van A",
			"age": 32,
			"address": "Nam Dinh"
		},
		"student_list": [
			{
				"fullname": "Tran Van A",
				"age": 19,
				"address": "Ha Noi"
			},
			{
				"fullname": "Tran Van A",
				"age": 19,
				"address": "Ha Noi"
			}
		]
	},
	{
		"classname": "T1907E",
		"address": "8 Ton That Thuyet, Cau Giay, HN",
		"teacher": {
			"fullname": "Tran Van B",
			"age": 32,
			"address": "Nam Dinh"
		},
		"student_list": [
			{
				"fullname": "Tran Van B",
				"age": 19,
				"address": "Ha Noi"
			},
			{
				"fullname": "Tran Van B",
				"age": 19,
				"address": "Ha Noi"
			}
		]
	}
]



Sử dụng bộ thư viên gson trong lập trình Java

B1. Download thu vien gson => add project
B2. Mapping class object
	=> Tao class object tuong ung vs file json
B3. Su dung thu vien gson => convert class object trong java


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)