介绍 Fastjson是一个Java语言编写的高
性能功能完善的JSON库。它采用一种“假定有序
快速匹配”的算法,把JSON Parse的
性能提升到极致,是目前Java语言中最快的JSON库。Fastjson接口简单易用,已经被广泛使用在缓存序列化、协议交互、Web
输出、Android客户端等多种应用场景。 教程 maven依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.24</version> </dependency> package com.zzg.entity; import java.io.Serializable; public class Address implements Serializable { /** * */ private static final long serialVersionUID = 1L; private int id; private String provice; private String city; private String area; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getProvice() { return provice; } public void setProvice(String provice) { this.provice = provice; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } } package com.zzg.entity; import java.io.Serializable; import java.util.List; public class User implements Serializable { /** * */ private static final long serialVersionUID = 1L; private int id; private String name; private int sex; private List<Address> list; 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 getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public List<Address> getList() { return list; } public void setList(List<Address> list) { this.list = list; } } 1、序列化 主要通过 JSON.toJSONString
方法将Bean转换为json。 //通过 JSON.toJSONString
方法将Bean转换为json。 User user =new User(); user.setId(1); user.setName("周志刚"); user.setSex(1); List<Address> address=new ArrayList<Address>(); Address one = new Address(); one.setArea("开福区"); one.setCity("长沙市"); one.setId(1); one.setProvice("湖南省"); address.add(one); Address two = new Address(); two.setArea("天河区"); two.setCity("广州市"); two.setId(1); two.setProvice("广东省"); address.add(two); user.setList(address); System.out.println("结果:"+JSON.toJSONString(user)); 结果如下: {"id":1,"list":[{"area":"开福区","city":"长沙市","id":1,"provice":"湖南省"},{"area":"天河区","city":"广州市","provice":"广东省"}],"name":"周志刚","sex":1} 2、反序列化 主要通过JSON.par
SEObject
方法将json转换为Bean对象,如下: //通过JSON.par
SEObject
方法将json转换为Bean对象 String json = "{\"id\":1,\"list\":[{\"area\":\"开福区\",\"city\":\"长沙市\",\"id\":1,\"provice\":\"湖南省\"},{\"area\":\"天河区\",\"city\":\"广州市\",\"provice\":\"广东省\"}],\"name\":\"周志刚\",\"sex\":1}"; User u = JSON.par
SEObject(json,User.class); System.out.println("
用户名次:"+u.getName()); 结果如下:
用户名次:周志刚 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的list
属性在序列化时变成addr_list,如下: package com.zzg.entity; import java.io.Serializable; import java.util.List; import com.alibaba.fastjson.annotation.JSONField; public class User implements Serializable { /** * */ private static final long serialVersionUID = 1L; private int id; private String name; private int sex; @JSONField(name="addr_list") private List<Address> list; 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 getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public List<Address> getList() { return list; } public void setList(List<Address> list) { this.list = list; } }
输出结果: {"addr_list":[{"area":"开福区","sex":1} 3.2 使用serialize/deserialize指定字段不序列化 控制sex
属性不序列化,如下: package com.zzg.entity; import java.io.Serializable; import java.util.List; import com.alibaba.fastjson.annotation.JSONField; public class User implements Serializable { /** * */ private static final long serialVersionUID = 1L; private int id; private String name; @JSONField(serialize=false) private int sex; @JSONField(name="addr_list") private List<Address> list; 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 getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public List<Address> getList() { return list; } public void setList(List<Address> list) { this.list = list; } } 结果
输出: {"addr_list":[{"area":"开福区","name":"周志刚"} 3.3 使用ordinal指定字段的顺序 package com.zzg.entity; import java.io.Serializable; import com.alibaba.fastjson.annotation.JSONField; public class Address implements Serializable { /** * */ private static final long serialVersionUID = 1L; @JSONField(ordinal = 4) private int id; @JSONField(ordinal = 1) private String provice; @JSONField(ordinal = 2) private String city; @JSONField(ordinal = 3) private String area; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getProvice() { return provice; } public void setProvice(String provice) { this.provice = provice; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } }
输出结果: {"addr_list":[{"provice":"湖南省","area":"开福区","id":1},{"provice":"广东省","area":"天河区","id":1}],"name":"周志刚"} 参考资料: https://github.com/alibaba/fastjson/wiki/JSONField
原文链接:https://www.f2er.com/json/288964.html