使用Fastjson实现JSON与JavaBean之间互相转换

本文详细介绍了如何使用Fastjson进行JSON与JavaBean之间的转换,包括简单JSON到JavaBean、JavaBean到JSON,JSON数组到JavaBean,JavaBean到JSON数组以及复杂JSON到JavaBean和JavaBean到复杂JSON的转换。通过实例代码展示了转换过程,并提供了运行效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接:http://www.yiidian.com/fastjson/fastjson-json-javabean.html

1 简单JSON与JavaBean的转换

1.1 设计Student实体类
package com.yiidian.domain;

/**
 * 一点教程网 - http://www.yiidian.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 简单JSON转为JavaBean

MainApp:

package com.yiidian.fastjson;

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

/**
 * 一点教程网 - http://www.yiidian.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);
    }

}

运行效果为:

Student{studentName='lily', studentAge=12}
1.3 JavaBean转为简单JSON

MainApp:

package com.yiidian.fastjson;

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

/**
 * 一点教程网 - http://www.yiidian.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);
    }

}

运行效果为:

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

2 JSON数组与JavaBean的转换

2.1 JSON数组转为JavaBean

MainApp:

package com.yiidian.fastjson;

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

import java.util.List;

/**
 * 一点教程网 - http://www.yiidian.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);
    }

}

运行结果为:

studentList:  [Student{studentName='lily', studentAge=12}, Student{studentName='lucy', studentAge=15}]
2.2 JavaBean转为JSON数组

MainApp:

package com.yiidian.fastjson;

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

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

/**
 * 一点教程网 - http://www.yiidian.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);
    }

}

运行效果为:

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

3 复杂JSON与JavaBean的转换

3.1 设计Course实体类
package com.yiidian.domain;

import java.util.List;

/**
 * 一点教程网 - http://www.yiidian.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 设计Teacher实体类
package com.yiidian.domain;

import java.util.List;

/**
 * 一点教程网 - http://www.yiidian.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 复杂JSON转为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;

/**
 * 一点教程网 - http://www.yiidian.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);
    }

}

运行效果为:

Teacher{teacherName='crystall', teacherAge=27, course=Course{courseName='english', code='1270'}, students=[Student{studentName='lily', studentAge=12}, Student{studentName='lucy', studentAge=15}]}
3.3 JavaBean转为复杂JSON

MainApp:

package com.yiidian.fastjson;

import com.alibaba.fastjson.JSONObject;
import com.yiidian.domain.Teacher;
/**
 * 一点教程网 - http://www.yiidian.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);
    }

}

运行效果为:

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

file

欢迎关注我的公众号::一点教程。获得独家整理的学习资源和日常干货推送。 如果您对我的系列教程感兴趣,也可以关注我的网站:yiidian.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值