介绍
Fastjson是一个Java语言编写的高性能功能完善的JSON库。它采用一种“假定有序快速匹配”的算法,把JSON Parse的性能提升到极致,是目前Java语言中最快的JSON库。Fastjson接口简单易用,已经被广泛使用在缓存序列化、协议交互、Web输出、Android客户端等多种应用场景。
@H_404_5@教程maven依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.12</version>
</dependency>
API使用
涉及到User和Address两个类,分别如下:
User
class User{
private int id;
private String name;
private int age;
private List<Address> addrList;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 List<Address> getAddrList() {
return addrList;
}
public void setAddrList(List<Address> addrList) {
this.addrList = addrList;
}
}
Address
class Address{
private String province;
private String city;
private String district;
private String detail;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
1、序列化
主要通过 JSON.toJSONString方法将Bean转换为json。
String com.alibaba.fastjson.JSON.toJSONString(Object object)
User user = new User();
user.setId(1);
user.setName("ricky");
user.setAge(27);
List<Address> addrList = new ArrayList<>();
Address addr1 = new Address();
addr1.setProvince("北京");
addr1.setCity("北京市");
addr1.setDistrict("朝阳区");
addr1.setDetail("大望路金地中心");
addrList.add(addr1);
Address addr2 = new Address();
addr2.setProvince("湖北省");
addr2.setCity("武汉市");
addr2.setDistrict("武昌区");
addr2.setDetail("江汉路步行街");
addrList.add(addr2);
user.setAddrList(addrList);
System.out.println(user);
String jsonStr = JSON.toJSONString(user);
System.out.println(jsonStr);
结果如下:
{“addrList”:[{“city”:”北京市”,”detail”:”大望路金地中心”,”district”:”朝阳区”,”province”:”北京”},{“city”:”武汉市”,”detail”:”江汉路步行街”,”district”:”武昌区”,”province”:”湖北省”}],”age”:27,”id”:1,”name”:”ricky”}
2、反序列化
主要通过JSON.parSEObject方法将json转换为Bean对象,如下:
<T> T com.alibaba.fastjson.JSON.parSEObject(String text,Class<T> clazz)
String jsonStr = "...";
User user = JSON.parSEObject(jsonStr,User.class);
System.out.println(user);
3、JSONField 介绍
package com.alibaba.fastjson.annotation;
public @interface JSONField {
// 配置序列化和反序列化的顺序,1.1.42版本之后才支持
int ordinal() default 0;
// 指定字段的名称
String name() default "";
// 指定字段的格式,对日期格式有用
String format() default "";
// 是否序列化
boolean serialize() default true;
// 是否反序列化
boolean deserialize() default true;
}
3.1 指定序列化名称
指定User的addrList属性在序列化时变成addr_list,如下:
class User{
private int id;
private String name;
private int age;
@JSONField(name="addr_list")
private List<Address> addrList;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 List<Address> getAddrList() {
return addrList;
}
public void setAddrList(List<Address> addrList) {
this.addrList = addrList;
}
}
结果:
{“addr_list”:[{“city”:”北京市”,”name”:”ricky”}
3.2 使用serialize/deserialize指定字段不序列化
控制age属性不序列化,如下:
class User{
private int id;
private String name;
@JSONField(serialize=false)
private int age;
@JSONField(name="addr_list")
private List<Address> addrList;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 List<Address> getAddrList() {
return addrList;
}
public void setAddrList(List<Address> addrList) {
this.addrList = addrList;
}
}
结果:
{“addr_list”:[{“city”:”北京市”,”name”:”ricky”}
3.3 使用ordinal指定字段的顺序
class User{
@JSONField(ordinal = 1)
private int id;
@JSONField(ordinal = 2)
private String name;
@JSONField(ordinal = 3)
private int age;
@JSONField(name="addr_list",ordinal=4)
private List<Address> addrList;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 List<Address> getAddrList() {
return addrList;
}
public void setAddrList(List<Address> addrList) {
this.addrList = addrList;
}
}
结果:
{“id”:1,”name”:”ricky”,”addr_list”:[{“city”:”北京市”,”province”:”湖北省”}]}