# fastjson
fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。
###使用
# 导入jar包
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
1
2
3
4
5
6
2
3
4
5
6
# 序列化
Student student = new Student(2, "warren", 10.0, new Date());
System.out.println(student);
System.out.println(student.toString());
//serialize
String json = JSONObject.toJSONString(student);
System.out.println(json);
1
2
3
4
5
6
7
2
3
4
5
6
7
# 反序列化
//deserialize
Student student1 = JSONObject.parseObject(json, Student.class);
System.out.println(student1);
System.out.println(student1.toString());
1
2
3
4
2
3
4
# 时间dateformat
时间字段序列化注解
@JSONField(format = "yyyyMMdd")
private Date birthday;
1
2
2
时间序列化
Date date1 = new Date();
System.out.println(JSON.toJSONStringWithDateFormat(date1, "yyyy-MM-dd"));
1
2
2
# 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;
// 支持反序列化时使用多个不同的字段名称
String[] alternateNames() default {};
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 相关资料
Fastjson中文手册 (opens new window)
全量分析 阅读量: