Json 数据处理技术 之 FastJson
- Maven依赖
com.alibaba
fastjson
1.2.62
测试代码实体类:
public class Dept {
private Integer deptno;
private String dname;
private String loc;
public Dept() {
}
public Dept(Integer deptno, String dname, String loc) {
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
@Override
public String toString() {
return "Dept{" +
"deptno=" + deptno +
", dname='" + dname + '\'' +
", loc='" + loc + '\'' +
'}';
}
}
注:采用FastJSON的实体类必须得实现getter/setter方法,否则序列化时会得不到数据,只会得到一个空的JSON对象
示例:实体类序列化和反序列化public static void main(String[] args) {
Dept dept = new Dept(10, "RESEARCH", "NEWYORK");
//序列化
String json = JSON.toJSONString(dept);
System.out.println(json);
//反序列化
Dept res = JSON.parseObject(json,Dept.class);
System.out.println(res.getDname());
}
示例:List/Set序列化和反序列化
public static void main(String[] args) {
Set depts = new HashSet();
depts.add(new Dept(10,"ACCOUNTING","CHICAGO"));
depts.add(new Dept(20,"RESEARCH","DALLAS"));
depts.add(new Dept(30,"SALES","CHICAGO"));
depts.add(new Dept(40,"OPERATIONS","BOSTON"));
//序列化
String json = JSON.toJSONString(depts);
System.out.println(json);
//反序列化
List res = JSON.parseArray(json, Dept.class);
res.forEach(System.out::println);
}
示例:Map序列化和反序列化
public static void main(String[] args) {
Map map = new HashMap();
map.put("ACCOUNTING",new Dept(10,"ACCOUNTING","CHICAGO"));
map.put("RESEARCH",new Dept(20,"RESEARCH","DALLAS"));
map.put("SALES",new Dept(30,"SALES","CHICAGO"));
map.put("OPERATIONS",new Dept(40,"OPERATIONS","BOSTON"));
//序列化
String json = JSON.toJSONString(map);
System.out.println(json);
//反序列化
Map res = JSONObject.parseObject(json, new TypeReference() {});
res.forEach((k,v)-> System.out.println(k+" : "+v));
}
常用注解
- @JSONField(ordinal =0):指定序列化字段的顺序,默认是0
- @JSONField(name = “”):指定序列化字段的别名
- @JSONField(format = “”):指定日期格式
- @JSONField(serialize = 布尔类型):是否要把这个字段序列化成JSON字符串,默认是true
- @JSONField(deserialize = 布尔类型):是否需要进行反序列化,默认是true
- 实体类:
public class Stu {
@JSONField(ordinal = 0) //指定序列化的顺序
private int id;
@JSONField(ordinal = 1,name = "stuName")//指定序列化后的字段名
private String name;
@JSONField(ordinal = 2,format = "yyyy-MM-DD")//日期格式化
private Date birth;
@JSONField(ordinal = 3,serialize = false,deserialize = false)//指定字段不序列化,不反序列化
private float score;
public Stu() {
}
public Stu(int id, String name, Date birth, float score) {
this.id = id;
this.name = name;
this.birth = birth;
this.score = score;
}
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 Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
@Override
public String toString() {
return "Stu{" +
"id=" + id +
", name='" + name + '\'' +
", birth=" + birth +
", score=" + score +
'}';
}
}
- 测试代码:
public static void main(String[] args) {
Stu stu = new Stu(1111, "zhangsan", new Date(), 88);
String json = JSON.toJSONString(stu);//序列化
System.out.println(json);
String data = "{'id':1111,'stuName':'zhangsan','birth':'2019-10-290','score':98}";
Stu res = JSON.parseObject(data, Stu.class);//反序列化
System.out.println(res);
}