Using Fastjson to transform JSON and JavaBean

Original link: http://www.yiidian.com/fastjson/fastjson-json-javabean.html

1. Transformation of simple JSON and JavaBean

1.1 design Student entity class

package com.yiidian.domain;

/**
 * A little tutorial website - http://www.yidian.com
 */
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 simple JSON to JavaBean

MainApp:

package com.yiidian.fastjson;

import com.alibaba.fastjson.JSONObject;
import com.yiidian.domain.Student;

/**
 * A little tutorial website - http://www.yidian.com
 */
public class MainApp {

    public static void main(String args[]){
        String  JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";

        Student student = JSONObject.parseObject(JSON_OBJ_STR, Student.class);

        System.out.println(student);
    }

}

The operation effect is:

Student{studentName='lily', studentAge=12}

1.3 turn JavaBeans into simple JSON

MainApp:

package com.yiidian.fastjson;

import com.alibaba.fastjson.JSONObject;
import com.yiidian.domain.Student;

/**
 * A little tutorial website - http://www.yidian.com
 */
public class MainApp {

    public static void main(String args[]){
        Student student = new Student("lily", 12);
        String jsonString = JSONObject.toJSONString(student);
        System.out.println(jsonString);
    }

}

The operation effect is:

{"studentAge":12,"studentName":"lily"}

2 conversion of JSON array and JavaBean

2.1 convert JSON array to JavaBean

MainApp:

package com.yiidian.fastjson;

import com.alibaba.fastjson.JSONArray;
import com.yiidian.domain.Student;

import java.util.List;

/**
 * A little tutorial website - http://www.yidian.com
 */
public class MainApp {

    public static void main(String args[]){

        String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";

        List<Student> studentList = JSONArray.parseArray(JSON_ARRAY_STR, Student.class);
        System.out.println("studentList:  " + studentList);
    }

}

The operation result is:

studentList:  [Student{studentName='lily', studentAge=12}, Student{studentName='lucy', studentAge=15}]

2.2 JavaBean to JSON array

MainApp:

package com.yiidian.fastjson;

import com.alibaba.fastjson.JSONArray;
import com.yiidian.domain.Student;

import java.util.ArrayList;
import java.util.List;

/**
 * A little tutorial website - http://www.yidian.com
 */
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);

        String jsonString = JSONArray.toJSONString(students);
        System.out.println(jsonString);
    }

}

The operation effect is:

[{"studentAge":12,"studentName":"lily"},{"studentAge":15,"studentName":"lucy"}]

3 Transformation of complex JSON and JavaBean

3.1 design Course entity class

package com.yiidian.domain;

import java.util.List;

/**
 * A little tutorial website - http://www.yidian.com
 */
public class Course {
    private String courseName;
    private String code;

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "Course{" +
                "courseName='" + courseName + '\'' +
                ", code='" + code + '\'' +
                '}';
    }
}

3.2 design the Teacher entity class

package com.yiidian.domain;

import java.util.List;

/**
 * A little tutorial website - http://www.yidian.com
 */
public class Teacher {
    private String teacherName;
    private Integer teacherAge;

    private Course course;
    private List<Student> students;

    public String getTeacherName() {
        return teacherName;
    }

    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 conversion of complex JSON to JavaBean

MainApp:

package com.yiidian.fastjson;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yiidian.domain.Student;
import com.yiidian.domain.Teacher;

import java.util.ArrayList;
import java.util.List;

/**
 * A little tutorial website - http://www.yidian.com
 */
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}]}";

        Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR, Teacher.class);
        System.out.println(teacher);
    }

}

The operation effect is:

Teacher{teacherName='crystall', teacherAge=27, course=Course{courseName='english', code='1270'}, students=[Student{studentName='lily', studentAge=12}, Student{studentName='lucy', studentAge=15}]}

3.3 turn JavaBeans into complex JSON

MainApp:

package com.yiidian.fastjson;

import com.alibaba.fastjson.JSONObject;
import com.yiidian.domain.Teacher;
/**
 * A little tutorial website - http://www.yidian.com
 */
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}]}";

        Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR, Teacher.class);

        String jsonString = JSONObject.toJSONString(teacher);
        System.out.println(jsonString);
    }

}

The operation effect is:

{"course":{"code":"1270","courseName":"english"},"students":[{"studentAge":12,"studentName":"lily"},{"studentAge":15,"studentName":"lucy"}],"teacherAge":27,"teacherName":"crystall"}

Welcome to my official account: a little tutorial. Get exclusive learning resources and daily dry goods push. If you are interested in my series of tutorials, you can also follow my website: yiidian.com

Tags: Programming JSON Java

Posted on Mon, 13 Apr 2020 07:35:06 -0700 by rimedey