对于JSON,大家常用的就是Gson、JackSon、FastSon,那么今天先来介绍利用JackSon进行JSON的序列化和解析。
首先,IDE我选择使用IDEA,那么自然也会使用MAVEN来管理依赖,一下是pox.xml的配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<
dependency
>
<
groupId
>com.fasterxml.jackson.core</
groupId
>
<
artifactId
>jackson-core</
artifactId
>
<
version
>2.6.3</
version
>
</
dependency
>
<
dependency
>
<
groupId
>com.fasterxml.jackson.core</
groupId
>
<
artifactId
>jackson-annotations</
artifactId
>
<
version
>2.6.3</
version
>
</
dependency
>
<
dependency
>
<
groupId
>com.fasterxml.jackson.core</
groupId
>
<
artifactId
>jackson-databind</
artifactId
>
<
version
>2.6.3</
version
>
</
dependency
>
|
一、首先使用不带注解的方式:
1、创建User类:
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
|
package
info.tshare.jacksondemo;
import
java.util.Date;
/**
* Created with IntelliJ IDEA.
* User: Tshare
* Date: 2015-11-24
* Time: 9:26
* JSON序列化和反序列化使用的User类
*/
public
class
User {
private
String name;
private
Integer age;
private
Date birthday;
private
String email;
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
Integer getAge() {
return
age;
}
public
void
setAge(Integer age) {
this
.age = age;
}
public
Date getBirthday() {
return
birthday;
}
public
void
setBirthday(Date birthday) {
this
.birthday = birthday;
}
public
String getEmail() {
return
email;
}
public
void
setEmail(String email) {
this
.email = email;
}
}
|
2、创建测试类:
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
|
package
info.tshare.jacksondemo;
import
com.fasterxml.jackson.core.JsonProcessingException;
import
com.fasterxml.jackson.databind.ObjectMapper;
import
java.io.IOException;
import
java.text.ParseException;
import
java.text.SimpleDateFormat;
import
java.util.List;
import
java.util.ArrayList;
/**
* Created with IntelliJ IDEA.
* User: Tshare
* Date: 2015-11-24
* Time: 9:28
*/
public
class
JacksonDemo {
public
static
void
main(String[] args)
throws
ParseException, IOException {
User user =
new
User();
user.setName(
"Tshare"
);
user.setEmail(
"tshareinfo@gmail.com"
);
user.setAge(
24
);
SimpleDateFormat dateFormat =
new
SimpleDateFormat(
"yyyy-MM-dd"
);
user.setBirthday(dateFormat.parse(
"1991-01-01"
));
/**
* ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。
* ObjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。
* writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。
* writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。
* writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。
* writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。
*/
ObjectMapper mapper =
new
ObjectMapper();
/**
* User类转JSON
* 输出结果{"name":"Tshare","age":24,"birthday":662659200000,"email":"tshareinfo@gmail.com"}
*/
String json = mapper.writeValueAsString(user);
System.out.println(json);
/**
* Java集合转Json
* 输出结果[{"name":"Tshare","age":24,"birthday":662659200000,"email":"tshareinfo@gmail.com"}]
*/
List<User> users =
new
ArrayList<User>();
users.add(user);
String jsonlist = mapper.writeValueAsString(users);
System.out.println(jsonlist);
String jsonstr =
"{\"name\":\"Tshare\",\"age\":24,\"birthday\":662659200000,\"email\":\"tshareinfo@gmail.com\"}"
;
/**
* ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。
*/
User userJson = mapper.readValue(jsonstr,User.
class
);
System.out.println(userJson);
}
}
|
二、测试使用注解的方式:
1、创建User类:
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
|
package
info.tshare.jacksondemo;
import
com.fasterxml.jackson.annotation.JsonFormat;
import
com.fasterxml.jackson.annotation.JsonIgnore;
import
com.fasterxml.jackson.annotation.JsonProperty;
import
java.util.Date;
/**
* Created with IntelliJ IDEA.
* User: Tshare
* Date: 2015-11-24
* Time: 9:26
* JSON序列化和反序列化使用的User类
*/
public
class
User {
private
String name;
@JsonIgnore
//不JSON序列化年龄
private
Integer age;
@JsonFormat
(pattern =
"yyyy年MM月dd日"
)
//格式化日期属性
private
Date birthday;
@JsonProperty
(
"mail"
)
//序列化email属性为mail
private
String email;
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
Integer getAge() {
return
age;
}
public
void
setAge(Integer age) {
this
.age = age;
}
public
Date getBirthday() {
return
birthday;
}
public
void
setBirthday(Date birthday) {
this
.birthday = birthday;
}
public
String getEmail() {
return
email;
}
public
void
setEmail(String email) {
this
.email = email;
}
}
|
2、创建测试类:
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
|
package
info.tshare.jacksondemo;
import
com.fasterxml.jackson.core.JsonProcessingException;
import
com.fasterxml.jackson.databind.ObjectMapper;
import
java.io.IOException;
import
java.text.ParseException;
import
java.text.SimpleDateFormat;
import
java.util.List;
import
java.util.ArrayList;
/**
* Created with IntelliJ IDEA.
* User: Tshare
* Date: 2015-11-24
* Time: 9:28
*/
public
class
JacksonDemo {
public
static
void
main(String[] args)
throws
ParseException, JsonProcessingException {
User user =
new
User();
user.setName(
"Tshare"
);
user.setEmail(
"tshareinfo@gmail.com"
);
user.setAge(
24
);
SimpleDateFormat dateFormat =
new
SimpleDateFormat(
"yyyy-MM-dd"
);
user.setBirthday(dateFormat.parse(
"1991-01-01"
));
ObjectMapper mapper =
new
ObjectMapper();
String json = mapper.writeValueAsString(user);
System.out.println(json);
}
}
|
执行结果:
{"name":"Tshare","birthday":"1990年12月31日","mail":"tshareinfo@gmail.com"}
https://blog.liyang.io/25.html
本文转自 stock0991 51CTO博客,原文链接:http://blog.51cto.com/qing0991/1943336