FastJson的简单使用

FastJson的简单使用

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:

关注文章首部微信公众号发送 #169_fastjson 获取依赖 jar 包。

JSON 字符串转 JSON 对象

@Test
public void test1() {
    // json 字符串 -> 简单对象型
    final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";
    JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
    System.out.println(jsonObject.getString("studentName") + ":" + jsonObject.getInteger("studentAge"));
}

@Test
public void test2() {
    // json 字符串 -> 数组类型
    final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
    JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);

    // 遍历方式1
    int size = jsonArray.size();
    for (int i = 0; i < size; i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        System.out.println(jsonObject.getString("studentName") + ":" + jsonObject.getInteger("studentAge"));
    }

    // 遍历方式2
    for (Object obj : jsonArray) {
        JSONObject jsonObject = (JSONObject) obj;
        System.out.println(jsonObject.getString("studentName") + ":" + jsonObject.getInteger("studentAge"));
    }
}

@Test
public void test3() {
    // 复杂格式 json 字符串 -> JSON对象
    final String COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
    JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
    String teacherName = jsonObject.getString("teacherName");
    Integer teacherAge = jsonObject.getInteger("teacherAge");
    JSONObject course = jsonObject.getJSONObject("course");
    JSONArray students = jsonObject.getJSONArray("students");
}

JSON 对象转 JSON 字符串:

String jsonStr = jsonObject.toJSONString();

JSON 字符串转 JavaBean 对象

// com.zze.bean.Course
public class Course {

    private String courseName;
    private Integer 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 +
                '}';
    }
}
// com.zze.bean.Student
public class Student {

    private String studentName;
    private Integer 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 +
                '}';
    }
}
// com.zze.bean.Teacher
import java.util.List;

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 +
                '}';
    }
}
@Test
public void test6() {
    // json 字符串 -> JavaBean 对象
    final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";
    Student student = JSON.parseObject(JSON_OBJ_STR, Student.class);
    System.out.println(student.getStudentName() + ":" + student.getStudentAge()); //lily:12
}

@Test
public void test7() {
    // json 字符串 -> JavaBean 集合对象
    final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
    ArrayList<Student> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {
    });
    for (Student student : students) {
        System.out.println(student.getStudentName() + ":" + student.getStudentAge());
    }
}

@Test
public void test8() {
    // json 字符串 -> 复杂 JavaBean 对象    
    final String COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
    Teacher teacher = JSON.parseObject(COMPLEX_JSON_STR, Teacher.class);
}

JavaBean 对象转 JSON 字符串:

String jsonStr = JSON.toJSONString(teacher);

JSON 对象和 JavaBean 对象转换

@Test
public void test9() {
    final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";
    JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
    // JSON 对象 -> JavaBean 对象
    Student student = jsonObject.toJavaObject(Student.class);
    System.out.println(student);
}
@Test
public void test10(){
    Student student = new Student();
    student.setStudentAge(18);
    student.setStudentName("bob");
    // JavaBean 对象 -> JSON 对象
    JSONObject o = (JSONObject)JSON.toJSON(student);
    String studentName = o.getString("studentName");
    System.out.println(studentName);
}

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://www.zze.xyz/archives/fastjson.html

Buy me a cup of coffee ☕.