Json、Gson、FastJson 快速入门
1、Json(JavaScript Object Notation)
1.1 简要介绍:
1.2 Json语法规则
-
对象表示为键值对
-
数据由逗号分隔
-
花括号保存对象
-
方括号保存数组
{"firstName": "Json"}
1.3 Json的基本使用方法
首先导入Json所需要的jar包
下载地址:百度云下载
将所需要的jar包导入到项目中

编写Json工具类
package com.dao;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.text.SimpleDateFormat;
public class JsonUtils {
public static String toJson(Object obj){
ObjectMapper mapper = new ObjectMapper();
SimpleDateFormat sdf = new SimpleDateFormat();
mapper.setDateFormat(sdf);
String result = null;
try {
result = mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return result;
}
}
Student类:
package com.vo;
public class Student {
private String name;
private int age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Student() {
}
public Student(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
测试类:
package com.main;
import com.dao.JsonUtils;
import com.vo.Student;
public class JsonTest {
public static void main(String[] args) {
Student student = new Student("张三",19,"男");
System.out.println(JsonUtils.toJson(student));
}
}
结果:

Json有什么优点:
1、轻量级的数据交换格式
2、人们读写更加容易
3、易于机器的解析和生成
4、能够通过JavaScript中eval()函数解析JSON
5、JSON支持多语言。包括:ActionScript, C, C#, ColdFusion, E, Java, JavaScript, ML, Objective CAML, Perl, PHP, Python, Rebol, Ruby, and Lua.
2、Gson
2.1 简要介绍
GSON是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库。可以将一个Json字符转成一个Java对象,或者将一个Java转化为Json字符串。
2.2 Json的基本使用方法
首先导入Gson所需要的jar包
下载地址:https://mvnrepository.com/artifact/com.google.code.gson/gson/2.8.5

如果是Maven项目 可直接在pom.xml中添加依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>

相对Json来说,Gson就可以不写工具类而直接实例出来了
测试类:
package com.main;
import com.google.gson.Gson;
import com.sun.xml.internal.ws.api.pipe.ServerTubeAssemblerContext;
import com.vo.Student;
public class GsonTest {
public static void main(String[] args) {
Student student = new Student("李四",20,"男");
Gson gson = new Gson();
System.out.println(gson.toJson(student));
}
}
结果:

Gson有什么优点:
3、FastJson
3.1 简要介绍
在日志解析,前后端数据传输交互中,经常会遇到字符串(String)与json,XML等格式相互转换与解析,其中json以跨语言,跨前后端的优点在开发中被频繁使用,基本上可以说是标准的数据交换格式。fastjson是一个java语言编写的高性能且功能完善的JSON库,它采用一种“假定有序快速匹配”的算法,把JSON Parse 的性能提升到了极致。它的接口简单易用,已经被广泛使用在缓存序列化,协议交互,Web输出等各种应用场景中。
3.2 FastJson 基本使用方法
首先导入Gson所需要的jar包
下载地址:https://mvnrepository.com/artifact/com.alibaba/fastjson


如果是Maven项目 可直接在pom.xml中添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
测试类:
package com.main;
import com.alibaba.fastjson.JSON;
import com.vo.Student;
public class FastJsonTest {
public static void main(String[] args) {
Student student = new Student("翠花",25,"女");
String studentJson = JSON.toJSONString(student);
System.out.println(studentJson);
}
}
结果:

FastJson 有什么优点:
1、快速FAST(比任何一款都快)
2、面向对象
3、功能强大(支持普通JDK类任意java bean Class,Collection,Map,Date或者enum)
4、零依赖(只需要有JDK即可)
5、支持注解,全类型序列化
FastJSON、Gson和Jackson性能对比
JSON序列化(Object => JSON)
测试样本数量为100000个,为了保证每个类库在测试中都能处理同一个样本,先把样本Java对象保存在文件中。每个类库测试3次,每次循环测试10遍,去掉最快速度和最慢速度,对剩下的8遍求平均值作为最终的速,取3次测试中最好的平均速度作为最终的测试数据。
| 类库 | 样本数量 | 执行次数 | 最长时间(毫秒) | 最短时间(毫秒) | 平均时间(毫秒) |
|---|---|---|---|---|---|
| FastJSON | 100000 | 10 | 2291.22 | 1416.70 | 1454.93 |
| Jackson | 100000 | 10 | 1980.92 | 841.91 | 880.82 |
| Gson | 100000 | 10 | 2383.02 | 1469.08 | 1520.38 |
从测试数据可知,FastJSON和GsonJSON序列化的速度差不多,Jackson是最快的(用时Gson少大约600毫秒)。
JSON反序列化(JSON => Object)
测试样本数量为100000个,为了保证每个类库在测试中都能处理同一个样本,先把样本JSON对象保存在文件中。每个类库测试3次,每次循环测试10遍,去掉最快速度和最慢速度,对剩下的8遍求平均值作为最终的速,取3次测试中最好的平均速度作为最终的测试数据。
| 类库 | 样本数量 | 执行次数 | 最长时间(毫秒) | 最短时间(毫秒) | 平均时间(毫秒) |
|---|---|---|---|---|---|
| FastJSON | 100000 | 10 | 7942.31 | 6340.55 | 6526.41 |
| Jackson | 100000 | 10 | 7957.22 | 6623.85 | 6815.41 |
| Gson | 100000 | 10 | 8235.15 | 7006.06 | 7364.75 |
从测试数据可知,三个类库在反序列化上性能比较接近,Gson稍微差一些。
总结
把Java对象JSON序列化,Jackson速度最快,在测试中比Gson快接近50%,FastJSON和Gson速度接近。
把JSON反序列化成Java对象,FastJSON、Jackson速度接近,Gson速度稍慢,不过差距很小。

浙公网安备 33010602011771号