introduce
Fastjson is a high-performance, full-featured JSON library written in the Java language. It uses an "assumed order fast matching" algorithm, the performance of the JSON parse to the extreme, is the current Java language, the fastest JSON library. The Fastjson interface is easy to use and has been widely used in a variety of scenarios such as cache serialization, protocol interaction, web output, and Android clients. Tutorials maven Dependencies
<dependency>
<groupId>com.alibaba</groupId>
<artifactid>fastjson</ artifactid>
<version>1.2.12</version>
</dependency>
API Use
Related to user and address two classes, respectively, as follows:
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. Serialization of
The bean is converted to JSON primarily through the Json.tojsonstring method.
User user = new user ();
User.setid (1);
User.setname ("Ricky");
User.setage (a);
list<address> addrlist = new arraylist<> ();
Address ADDR1 = new address ();
Addr1.setprovince ("Beijing");
Addr1.setcity ("Beijing");
Addr1.setdistrict ("Chaoyang District");
Addr1.setdetail ("The Center of the Golden Land of Da Wang Lu");
Addrlist.add (ADDR1);
Address ADDR2 = new address ();
Addr2.setprovince ("Hubei Province");
Addr2.setcity ("Wuhan City");
Addr2.setdistrict ("Wuchang District");
Addr2.setdetail ("Jianghan Road pedestrian Street");
Addrlist.add (ADDR2);
User.setaddrlist (addrlist);
SYSTEM.OUT.PRINTLN (user);
String jsonstr = json.tojsonstring (user);
System.out.println (JSONSTR);
The results are as follows:
{"Addrlist": [{"City": "Beijing", "detail": "Da Wang Road Gold Center", "district": "Chaoyang District", "Province": "Beijing"},{"City": "Wuhan", "detail": "Jianghan Road pedestrian Street "," District ":" Wuchang District "," Province ":" Hubei Province "}," age ":," id ": 1," name ":" Ricky "} 2, deserialization
JSON is converted to a Bean object primarily through the Json.parseobject method, as follows:
String jsonstr = "...";
User user = Json.parseobject (jsonstr, user.class);
SYSTEM.OUT.PRINTLN (user);
3, Jsonfield introduction
Package com.alibaba.fastjson.annotation;
The public @interface Jsonfield {
//configuration serialization and deserialization order, after version 1.1.42 to support
int ordinal () default 0;
Specifies the name of the field,
String name () default "";
Specifies the format of the field, which is useful for date format
String format () default "";
Whether
Boolean serialize () default true is serialized;
Whether to deserialize
Boolean deserialize () default true;
3.1 Specifying the serialization name
The Addrlist property of the specified user becomes addr_list when serialized, as follows:
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;
}
}
Results:
{"Addr_list": [{"City": "Beijing", "detail": "Da Wang Road Gold Center", "district": "Chaoyang District", "Province": "Beijing"},{"City": "Wuhan", "detail": " Jianghan Road Pedestrian Street "," District ":" Wuchang District "," Province ":" Hubei Province "}]," Age ":", id ": 1," name ":" Ricky "} 3.2 use serialize/ Deserialize specifies that the field is not serialized
Control The age attribute is not serialized as follows:
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;
}
}
Result:
{"Addr_list": [{"City": "Beijing", "detail": "Da Wang Road Gold Center", "district": "Chaoyang District", "Province": "Beijing"},{"City": "Wuhan", " Detail ":" Jianghan Road pedestrian street, "district": "Wuchang District", "Province": "Hubei Province"}, "id": 1, "name": "Ricky"} 3.3 use ordinal to specify the order of fields
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;
The 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; }
}
Results:
{"id": 1, "name": "Ricky", "Age": "Addr_list": [{"City": "Beijing", "detail": "Da Wang Road Gold Center", "district": "Chaoyang District", "Province": "Beijing" },{"City": "Wuhan", "detail": "Jianghan Road pedestrian Street", "District": "Wuchang District", "Province": "Hubei Province"}}
Resources:
Https://github.com/alibaba/fastjson/wiki/JSONField