A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 阿莱 于 2019-9-25 16:04 编辑

【上海校区】三种常用的JSON转换


一、JSON的概述

我们做项目,通常会使用前后端分离的ajax交互,那么前后端交互的数据格式通常是json格式。这里既有牵扯到json和对象集合之间的转换,目前市面上的json转换主要有三种:


  • Jackson转换
  • FastJson转换
  • Gson转换


下面将会对这三种转换进行简单的入门演示:



二、准备工作

1、新建一个maven工程

工程名任意,打包方式选择jar


2、在pom.xml导入下面依赖

[XML] 纯文本查看 复制代码
 <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>


3、创建用于演示的类

[Java] 纯文本查看 复制代码
public class Student implements Serializable {

    private String sid;
    private String name;
    private Integer age;
    private String address;

    public Student() {
    }

    public Student(String sid, String name, Integer age, String address) {
        this.sid = sid;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid='" + sid + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }
}


三、案例演示

1、JackSon转换

[Java] 纯文本查看 复制代码
public class JackSonTest {

    //对象和json字符串之间的转换
    @Test
    public void jsonAndJsonStr() throws IOException {

        //创建学生的对象
        Student student = new Student();
        student.setSid("1001");
        student.setName("张无忌");
        student.setAge(18);
        student.setAddress("冰火岛");

        //创建jackson的转换对象
        ObjectMapper om = new ObjectMapper();
        //将对象转为json字符串
        String studentStr = om.writeValueAsString(student);
        System.out.println(studentStr);

        //将json字符串转为对象
        Student stu = om.readValue(studentStr, Student.class);

        System.out.println("stu = " + stu);
    }

    //list集合和json字符串之间的转换
    @Test
    public void listAndJsonStr() throws IOException {

        //创建list集合
        List<Student> studentList = new ArrayList<>();

        studentList.add(new Student("1001","刘德华",22,"香港"));
        studentList.add(new Student("1002","张学友",21,"香港"));
        studentList.add(new Student("1003","郭富城",18,"香港"));
        studentList.add(new Student("1004","黎明",19,"香港"));

        //创建jackson的转换对象
        ObjectMapper om = new ObjectMapper();
        String listStr = om.writeValueAsString(studentList);

        System.out.println(listStr);

        //json字符串转换为集合
        List<Student> list = om.readValue(listStr, List.class);

        System.out.println(list);
    }
}


2、FastJson装换

[Java] 纯文本查看 复制代码
public class FastJsonTest {

    //对象和json字符串之间的转换
    @Test
    public void jsonAndJsonStr() throws IOException {

        //创建学生的对象
        Student student = new Student();
        student.setSid("1001");
        student.setName("张无忌");
        student.setAge(18);
        student.setAddress("冰火岛");

        //创建fastjson的转换对象
        JSONObject jsonObject = new JSONObject();
        //将对象转换为json字符串
        String studentStr = jsonObject.toJSONString(student);

        System.out.println(studentStr);

        //将json字符串解析为对象
        Student stu = JSON.parseObject(studentStr, Student.class);

        System.out.println(stu);

    }

    //list集合和json字符串之间的转换
    @Test
    public void listAndJsonStr() throws IOException {

        //创建list集合
        List<Student> studentList = new ArrayList<>();

        studentList.add(new Student("1001","刘德华",22,"香港"));
        studentList.add(new Student("1002","张学友",21,"香港"));
        studentList.add(new Student("1003","郭富城",18,"香港"));
        studentList.add(new Student("1004","黎明",19,"香港"));

        //创建fastjson的转换对象
        JSONObject jsonObject = new JSONObject();

        //将集合转换为json字符串
        String listStr = jsonObject.toJSONString(studentList);

        System.out.println(listStr);

        //将json字符串转换为集合
        List<Student> list = JSON.parseArray(listStr, Student.class);

        System.out.println(list);
    }
}


3、Gson转换

[Java] 纯文本查看 复制代码
public class GsonTest {

    //对象和json字符串之间的转换
    @Test
    public void jsonAndJsonStr() throws IOException {

        //创建学生的对象
        Student student = new Student();
        student.setSid("1001");
        student.setName("张无忌");
        student.setAge(18);
        student.setAddress("冰火岛");

        //创建json转换对象
        Gson gson = new Gson();

        //将对象转换为json字符串
        String studentStr = gson.toJson(student);

        System.out.println(studentStr);

        //将json字符串转换为对象
        Student stu = gson.fromJson(studentStr, Student.class);

        System.out.println(stu);
    }

    //list集合和json字符串之间的转换
    @Test
    public void listAndJsonStr() throws IOException {

        //创建list集合
        List<Student> studentList = new ArrayList<>();

        studentList.add(new Student("1001","刘德华",22,"香港"));
        studentList.add(new Student("1002","张学友",21,"香港"));
        studentList.add(new Student("1003","郭富城",18,"香港"));
        studentList.add(new Student("1004","黎明",19,"香港"));

        //创建json转换对象
        Gson gson = new Gson();

        //将集合转换为json字符串
        String listStr = gson.toJson(studentList);

        System.out.println(listStr);

        //将json字符串转换为集合
        List<Student> list = gson.fromJson(listStr, List.class);

        System.out.println(list);
    }
}


四、小结

Gson性能最好,当不断增加的时候到了100000,Gson明细弱于Jackson和FastJson, 这时候FastJson性能是真的牛,另外还可以看到不管数量少还是多,Jackson一直表现优异。


数据量转换比较小时,建议使用Gson,其它情况考虑另外两种。



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马