FastJson序列化和反序列化


fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发。
官方下载地址https://github.com/alibaba/fastjson

主要特点:

1.快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson)

2.强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum)

3.零依赖(没有依赖其它任何类库除了JDK)

示例代码

1.实体类

普通实体类 User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package net.hifor.demo;

public class User {
private String userid;
private String name;
private int[] departments;
private String position;
private String mobile;
private String gender;
private String email;
private String weixinid;
private String avatar_mediaid;
private String extattr;

public void setUserid(String userid) {
this.userid = userid;
}

public String getUserid() {
return this.userid;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

public int[] getDepartments() {
return departments;
}

public void setDepartments(int[] departments) {
this.departments = departments;
}

public void setPosition(String position) {
this.position = position;
}

public String getPosition() {
return this.position;
}

public void setMobile(String mobile) {
this.mobile = mobile;
}

public String getMobile() {
return this.mobile;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getGender() {
return this.gender;
}

public void setEmail(String email) {
this.email = email;
}

public String getEmail() {
return this.email;
}

public void setWeixinid(String weixinid) {
this.weixinid = weixinid;
}

public String getWeixinid() {
return this.weixinid;
}

public void setAvatar_mediaid(String avatar_mediaid) {
this.avatar_mediaid = avatar_mediaid;
}

public String getAvatar_mediaid() {
return this.avatar_mediaid;
}

public void setExtattr(String extattr) {
this.extattr = extattr;
}

public String getExtattr() {
return this.extattr;
}
}
泛型类 Result.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package net.hifor.demo;


public class Result<T> {

private String errorcode;
private String errormes;
private T data;


public String getErrorcode() {
return errorcode;
}

public void setErrorcode(String errorcode) {
this.errorcode = errorcode;
}

public String getErrormes() {
return errormes;
}

public void setErrormes(String errormes) {
this.errormes = errormes;
}

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}
}

2.使用FastJson解析JSON

序列化,并格式化JSON字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//初始化对象
User user = new User();
user.setUserid("1001001");
user.setName("张三");
user.setDepartments(new int[]{1,2});
user.setPosition("产品经理");
user.setMobile("15913215421");
user.setGender("1");
user.setEmail("[email protected]");
user.setWeixinid("zhangsan4dev");
user.setAvatar_mediaid("2-G6nrLmr5EC3MNb_-zL1dDdzkd0p7cNliYu9V5w7o8K0");

//对象序列化
String json = JSONObject.toJSONString(user);
//对象序列化并格式化
System.out.println(JSONObject.toJSONString(user, true));
反序列化普通对象,
1
2
3
//普通对象反序列化
User deserializableUser = JSONObject.parseObject(json, User.class);
System.out.println("姓名:" + deserializableUser.getName());
反序列化泛型对象,针对泛型类型,需要使用TypeReference进行解析
1
2
3
4
5
json = "{\"errorcode\":\"0\",\"errormes\":\"ok\",\"data\":"+json+"}";
//泛型对象反序列化
Result<User> result = JSONObject.parseObject(json, new TypeReference<Result<User>>(){});
System.out.println("手机:" + result.getData().getMobile());
System.out.println(JSONObject.toJSONString(result, true));

原文地址:http://hifor.net/posts/20150927-a5c0330a.html

0%