fastjson2介绍
FASTJSON v2是FASTJSON项目的重要升级,目标是为下一个十年提供一个高性能的JSON库。通过同一套API,- 支持JSON/JSONB两种协议,JSONPath 是一等公民。
- 支持全量解析和部分解析。
- 支持Java服务端、客户端Android、大数据场景。
- 支持Kotlin
- 支持JSON Schema https://alibaba.github.io/fastjson2/json_schema_cn
- 支持Android (2.0.19.android)
- 支持Graal Native-Image (2.0.19.graal)
fastjson2下载
fastjson2 包下载地址:https://repo1.maven.org/maven2/com/alibaba/fastjson2/fastjson2/2.0.19/fastjson2-2.0.19.jar
fastjson2安装
1. Idea中导入fastjson2 包的方法。ctrl+alt+shift+S 打开项目结构,依次点击Libraries->+加号。
将刚才下载的jar文件导入我们的项目
2.maven导入fastjson2
<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.19</version>
</dependency>
Java类转换为json字符串
import com.alibaba.fastjson2.JSON;
import java.util.ArrayList;
import java.util.List;
/**
*Java对象转json字符串
*/
public class StringToJSON {
public static void main(String[] args) {
//定义JSON字符串
Employee e = new Employee("易学教程","male", 22);
System.out.println(JSON.toJSONString(e));
Employee e2 = new Employee("易学教程2","male", 23);
Employee e3 = new Employee("易学教程3","male", 25);
List<Employee> list = new ArrayList<>();
list.add(e);
list.add(e2);
list.add(e3);
System.out.println(JSON.toJSONString(list));
}
}
//JavaBean.java
class Employee{
private String name;
private String sex;
private Integer age;
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
public Employee(String name, String sex, Integer age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
输出
{"age":22,"name":"易学教程","sex":"male"}
[{"age":22,"name":"易学教程","sex":"male"},{"age":23,"name":"易学教程2","sex":"male"},{"age":25,"name":"易学教程3","sex":"male"}]
[{"age":22,"name":"易学教程","sex":"male"},{"age":23,"name":"易学教程2","sex":"male"},{"age":25,"name":"易学教程3","sex":"male"}]
Json字符串转Java对象
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import java.util.List;
/**
*解析Json字符串反序列化为Java对象
*/
public class StringToJSON {
public static void main(String[] args) {
//定义JSON字符串
String jsonStr = "{\"age\":22,\"name\":\"易学教程\",\"sex\":\"male\"}";
Employee e = (Employee) JSON.parseObject(jsonStr, Employee.class);//简单java类解析
System.out.println("json字符串转简单java对象:"+e.toString());
//解析带有list的json字符串
String jsonStr2 = "[{\"age\":22,\"name\":\"易学教程\",\"sex\":\"male\"},{\"age\":23,\"name\":\"易学教程2\",\"sex\":\"male\"},{\"age\":25,\"name\":\"易学教程3\",\"sex\":\"male\"}]";
List<Employee> list =
JSONObject.parseObject(jsonStr2, new TypeReference<List<Employee>>(){});
System.out.println(list);
}
}
//JavaBean.java
class Employee{
private String name;
private String sex;
private Integer age;
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
public Employee() {
}
public Employee(String name, String sex, Integer age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
json字符串转简单java对象:Employee{name='易学教程', sex='male', age=22}
[Employee{name='易学教程', sex='male', age=22}, Employee{name='易学教程2', sex='male', age=23}, Employee{name='易学教程3', sex='male', age=25}]
[Employee{name='易学教程', sex='male', age=22}, Employee{name='易学教程2', sex='male', age=23}, Employee{name='易学教程3', sex='male', age=25}]
注意:JSON.parseObject 对象中需要一个默认的构造函数public Employee() {} 。