Fastjson JSONObject与JavaBean转换
精华
小牛编辑
114浏览
2023-03-14
1 简单JavaBean与JSONObject转换
1.1 设计Student实体类
package cn.xnip.domain;
/**
* 小牛知识库网 - https://www.xnip.cn
*/
public class Student {
private String studentName;
private Integer studentAge;
public Student() {
}
public Student(String studentName, Integer studentAge) {
this.studentName = studentName;
this.studentAge = studentAge;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getStudentAge() {
return studentAge;
}
public void setStudentAge(Integer studentAge) {
this.studentAge = studentAge;
}
@Override
public String toString() {
return "Student{" +
"studentName='" + studentName + '\'' +
", studentAge=" + studentAge +
'}';
}
}
1.2 简单JavaBean转为JSONObject
MainApp:
package cn.xnip.fastjson;
import com.alibaba.fastjson.JSONObject;
import cn.xnip.domain.Student;
/**
* 小牛知识库网 - https://www.xnip.cn
*/
public class MainApp {
public static void main(String args[]){
Student student = new Student("lily", 12);
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(student);
System.out.println(jsonObject);
}
}
运行效果为:
{"studentAge":12,"studentName":"lily"}
1.3 JSONObject转为简单JavaBean
MainApp:
package cn.xnip.fastjson;
import com.alibaba.fastjson.JSONObject;
import cn.xnip.domain.Student;
/**
* 小牛知识库网 - https://www.xnip.cn
*/
public class MainApp {
public static void main(String args[]){
String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";
JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);
Student student = JSONObject.parseObject(jsonObject.toJSONString(), Student.class);
System.out.println(student);
}
}
运行效果为:
Student{studentName='lily', studentAge=12}
2 List集合与JSONArray转换
2.1 List集合转为JSONArray
MainApp:
package cn.xnip.fastjson;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.xnip.domain.Student;
import java.util.ArrayList;
import java.util.List;
/**
* 小牛知识库网 - https://www.xnip.cn
*/
public class MainApp {
public static void main(String args[]){
Student student = new Student("lily", 12);
Student studenttwo = new Student("lucy", 15);
List<Student> students = new ArrayList<Student>();
students.add(student);
students.add(studenttwo);
JSONArray jsonArray = (JSONArray) JSONArray.toJSON(students);
System.out.println(jsonArray);
}
}
运行效果为:
[{"studentAge":12,"studentName":"lily"},{"studentAge":15,"studentName":"lucy"}]
2.2 JSONArray转为List集合
MainApp:
package cn.xnip.fastjson;
import com.alibaba.fastjson.JSONArray;
import cn.xnip.domain.Student;
import java.util.List;
/**
* 小牛知识库网 - https://www.xnip.cn
*/
public class MainApp {
public static void main(String args[]){
String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);
List<Student> students = JSONArray.parseArray(jsonArray.toJSONString(), Student.class);
System.out.println(students);
}
}
运行效果为:
[Student{studentName='lily', studentAge=12}, Student{studentName='lucy', studentAge=15}]
3 复杂JavaBean与JSONObject转换
3.1 设计Course实体类
package cn.xnip.domain;
import java.util.List;
/**
* 小牛知识库网 - https://www.xnip.cn
*/
public class Course {
private String courseName;
private Integer code;
public Course() {
}
public Course(String courseName, Integer code) {
this.courseName = courseName;
this.code = code;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
@Override
public String toString() {
return "Course{" +
"courseName='" + courseName + '\'' +
", code='" + code + '\'' +
'}';
}
}
3.2 设计Teacher实体类
package cn.xnip.domain;
import java.util.List;
/**
* 小牛知识库网 - https://www.xnip.cn
*/
public class Teacher {
private String teacherName;
private Integer teacherAge;
private Course course;
private List<Student> students;
public String getTeacherName() {
return teacherName;
}
public Teacher() {
}
public Teacher(String teacherName, Integer teacherAge, Course course, List<Student> students) {
this.teacherName = teacherName;
this.teacherAge = teacherAge;
this.course = course;
this.students = students;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public Integer getTeacherAge() {
return teacherAge;
}
public void setTeacherAge(Integer teacherAge) {
this.teacherAge = teacherAge;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Teacher{" +
"teacherName='" + teacherName + '\'' +
", teacherAge=" + teacherAge +
", course=" + course +
", students=" + students +
'}';
}
}
3.3 复杂JavaBean转为JSONObject
MainApp:
package cn.xnip.fastjson;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.xnip.domain.Course;
import cn.xnip.domain.Student;
import cn.xnip.domain.Teacher;
import java.util.ArrayList;
import java.util.List;
/**
* 小牛知识库网 - https://www.xnip.cn
*/
public class MainApp {
public static void main(String args[]){
Student student = new Student("lily", 12);
Student studenttwo = new Student("lucy", 15);
List<Student> students = new ArrayList<Student>();
students.add(student);
students.add(studenttwo);
Course course = new Course("english", 1270);
Teacher teacher = new Teacher("crystall", 27, course, students);
JSONObject jsonObject = (JSONObject) JSONObject.toJSON(teacher);
System.out.println(jsonObject);
}
}
运行效果为:
{"teacherAge":27,"teacherName":"crystall","course":{"courseName":"english","code":1270},"students":[{"studentAge":12,"studentName":"lily"},{"studentAge":15,"studentName":"lucy"}]}
3.4 JSONObject转为复杂JavaBean
MainApp:
package cn.xnip.fastjson;
import com.alibaba.fastjson.JSONObject;
import cn.xnip.domain.Teacher;
/**
* 小牛知识库网 - https://www.xnip.cn
*/
public class MainApp {
public static void main(String args[]){
String COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
Teacher teacher = JSONObject.parseObject(jsonObject.toJSONString(), Teacher.class);
System.out.println(teacher);
}
}
运行效果为:
Teacher{teacherName='crystall', teacherAge=27, course=Course{courseName='english', code='1270'}, students=[Student{studentName='lily', studentAge=12}, Student{studentName='lucy', studentAge=15}]}