Fastjson--json/JSONObject/Bean常用转换

本文给出了Fastjson的GitHub地址,通过创建Teacher、Course、Student等实体类和几个JSON字符串,进行了实体类与JSON字符串的转换示例,并给出了测试代码。

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

首先贴上fastjson的guthub地址:https://github.com/alibaba/fastjson

 

本文先创建几个实体类和几个json字符串,然后做转换示例


1.创建实体类Teacher.java

package com.imooc.sell.json;
 
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;
    }
 
    public Teacher(String teacherName, Integer teacherAge, Course course, List<Student> students) {
        this.teacherName = teacherName;
        this.teacherAge = teacherAge;
        this.course = course;
        this.students = students;
    }
 
    public Teacher() {
    }
 
    @Override
    public String toString() {
        return "Teacher{" +
                "teacherName='" + teacherName + '\'' +
                ", teacherAge=" + teacherAge +
                ", course=" + course +
                ", students=" + students +
                '}';
    }
}


2.创建实体类Course.java


package com.imooc.sell.json;
 
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;
    }
 
    public Course() {
    }
 
    public Course(String courseName, Integer code) {
        this.courseName = courseName;
        this.code = code;
    }
 
    @Override
    public String toString() {
        return "Course{" +
                "courseName='" + courseName + '\'' +
                ", code=" + code +
                '}';
    }
}


3.创建实体类Student.java


package com.imooc.sell.json;
 
public class Student {
 
    private String studentName;
    private Integer studentAge;
 
    public Student() {
    }
 
    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;
    }
 
    public Student(String studentName, Integer studentAge) {
        this.studentName = studentName;
        this.studentAge = studentAge;
    }
 
    @Override
    public String toString() {
        return "Student{" +
                "studentName='" + studentName + '\'' +
                ", studentAge=" + studentAge +
                '}';
    }
}


4.创建测试代码
package com.imooc.sell.json;


/*
*
* 学习fastjson
* Date:2018-08-01
* */
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class FastJsonTest {
 
    //json字符串-简单对象型
    private static final String  JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";
 
    //json字符串-数组类型
    private static final String  JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
 
    //复杂格式json字符串
    private static final String  COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
 
 
/*
* 1.JSON格式字符串与JSON对象之间的转换
*/
 
    @Test
    public void testComplexJSONStrToJSONObject(){
 
        /*
        * 复杂json格式字符串与JSONObject之间的转换
        * */
        JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
        //获取单值
        System.out.println(jsonObject.get("teacherName"));
        //获取json对象
        JSONObject course = jsonObject.getJSONObject("course");
        System.out.println(course.get("courseName"));
        //获取数组
        JSONArray students = jsonObject.getJSONArray("students");
        System.out.println(students.getJSONObject(1).get("studentName"));
 
    }
 
    /**
     * 复杂JSONObject到json格式字符串的转换
     */
    @Test
    public void testJSONObjectToComplexJSONStr() {
        //复杂JSONObject,目标要转换为json字符串
        JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);
 
        String string = jsonObject.toString();
        System.out.println(string);
 
    }
 
    /*
    *
    *2.JSON格式字符串与javaBean之间的转换
    *
    */
    @Test
    public void testComplexJSONStrToJavaBean(){
        /**
         * 复杂json格式字符串到JavaBean_obj的转换
         */
        //使用Gson思想
        Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR,Teacher.class);
 
        System.out.println(teacher.getTeacherName());
        System.out.println(teacher.getCourse().getCourseName());
        System.out.println(teacher.getStudents().get(1).getStudentName());
 
    }
 
    /**
     * 复杂JavaBean_obj到json格式字符串的转换
     */
 
    @Test
    public void testJavaBeanToComplexJSONStr(){
        //先转成Bean
        Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR,Teacher.class);
 
        //转成json字符串
        String jsonString = JSONObject.toJSONString(teacher);
        System.out.println(jsonString);
    }
 
    /*
    *
    * 3.javaBean与json对象间的之间的转换
    *
    * */
 
    /**
     * 复杂JavaBean_obj到json对象的转换
     */
    @Test
    public void testComplexJavaBeanToJSONObject(){
        //先转成Bean
        Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR,Teacher.class);
 
        //转成json对象
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(teacher);
        System.out.println(jsonObject.getJSONObject("course").get("courseName"));
    }
 
 
    /**
     * 复杂json对象到JavaBean_obj的转换
     */
    @Test
    public void testComplexJSONObjectToJavaBean(){
 
        //先转成Bean
        Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR,Teacher.class);
 
        //转成json对象
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(teacher);
 
        //转成Bean
        Teacher teacher1 = JSONObject.parseObject(JSONObject.toJSONString(jsonObject), Teacher.class);
 
        System.out.println(teacher1.getTeacherName());
        System.out.println(teacher1.getCourse().getCourseName());
        System.out.println(teacher1.getStudents().get(1).getStudentName());
 
 
    }
 
}


原文:https://blog.youkuaiyun.com/a1032818891/article/details/81334874 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值