//关键
首先将json字符串转换为json对象,然后再解析json对象,过程如下
String s = JSON.toJSONString(person);
案例:
学生列:
public class Student {
private String name;
private int age;
private String phone;
public Student() {
}
public Student(String name, int age, String phone) {
this.name = name;
this.age = age;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
public class SchoolClass {
private ClassMsg cm;
private String app;
List<Student> students;
public SchoolClass() {
}
public SchoolClass(ClassMsg cm, String app, List<Student> students) {
this.cm = cm;
this.app = app;
this.students = students;
}
public ClassMsg getCm() {
return cm;
}
public void setCm(ClassMsg cm) {
this.cm = cm;
}
public String getApp() {
return app;
}
public void setApp(String app) {
this.app = app;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public class Person {
private int age;
private String name;
private Date birthday;
public Person() {
}
public Person(int age, String name, Date birthday) {
this.age = age;
this.name = name;
this.birthday = birthday;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public class ClassMsg {
private int num; //班级人数
private Date startDate; //开班日期
private String leader;
private String teacher;
public ClassMsg() {
}
public ClassMsg(int num, Date startDate, String leader, String teacher) {
this.num = num;
this.startDate = startDate;
this.leader = leader;
this.teacher = teacher;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public String getLeader() {
return leader;
}
public void setLeader(String leader) {
this.leader = leader;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
}
测试类:
public class FastJsonDemo {
public static void main(String[] args) {
Person person = new Person();
person.setAge(36);
person.setName("张三");
person.setBirthday(new Date());
Person person2 = new Person();
person2.setAge(26);
person2.setName("李四");
person2.setBirthday(new Date());
//转换
String s = JSON.toJSONString(person);
System.out.println(s);
ArrayList list = new ArrayList<>();
list.add(person);
list.add(person2);
String s1 = JSON.toJSONString(list);
System.out.println(s1);
SchoolClass sc = new SchoolClass();
ClassMsg cm = new ClassMsg();
ArrayList stuInfo = new ArrayList<>();
stuInfo.add(new Student("a",21,"21235545"));
stuInfo.add(new Student("b",25,"61551515"));
stuInfo.add(new Student("c",28,"16815154"));
cm.setLeader("rw");
cm.setNum(25);
cm.setStartDate(new Date());
cm.setTeacher("lx");
sc.setCm(cm);
sc.setApp("fastJson");
sc.setStudents(stuInfo);
System.out.println(JSON.toJSONString(sc));
}
}
结果:
{"age":36,"birthday":1620908540868,"name":"张三"}
[{"age":36,"birthday":1620908540868,"name":"张三"},{"age":26,"birthday":1620908540868,"name":"李四"}]
{"app":"fastJson","cm":{"leader":"rw","num":25,"startDate":1620908541236,"teacher":"lx"},"students":[{"age":21,"name":"a","phone":"21235545"},{"age":25,"name":"b","phone":"61551515"},{"age":28,"name":"c","phone":"16815154"}]}