FastJson基本使用
【摘要】 1. FastJson序列化API 1.1 序列化Java对象 1.2 序列化List集合 1.3 序列化Map集合 2. FashJson反序列化API 2.1 反序列化Java对象 2.2 反序列化List集合 2.3 反序列化Map集合 3. SerializerFeature枚举 3.1 WriteMapNullValue 3.2 WriteNullStringAsEmpty 3....
1. FastJson序列化API
- 方法:
JSON.toJSONString
- 序列化 : 是指将Java对象转成json格式字符串的过程。JavaBean对象、List集合对象、Map集合 为应用最广泛的。
1.1 序列化Java对象
- Java 中的 Student 对象 序列化 为 JSON 格式 字符串
@Test
public void objectToJson(){
Student student = new Student();
student.setId(1);
student.setName("兮动人");
student.setAge(24);
student.setAddress("深圳市");
student.setFlag(true);
String jsonString = JSON.toJSONString(student);
System.out.println(jsonString);
}
{
"address":"深圳市",
"age":24,
"flag":true,
"id":1,
"name":"兮动人"
}
1.2 序列化List集合
@Test
public void listToJson(){
Student student = new Student();
student.setId(1);
student.setName("兮动人");
student.setAge(24);
student.setAddress("深圳市");
student.setFlag(true);
Student student2 = new Student();
student2.setId(2);
student2.setName("Mike");
student2.setAge(25);
student2.setAddress("北京市");
student2.setFlag(false);
List<Student> list = new ArrayList<Student>();
list.add(student);
list.add(student2);
String jsonString = JSON.toJSONString(list);
System.out.println(jsonString);
}
[{
"address":"深圳市","age":24,"flag":true,
"id":1,"name":"兮动人"},{"address":"北京市","age":25,"flag":false,"id":2,"name":"Mike"}]
- 可以观察到 JSON 默认排序是按
a-z
字母顺序排的
[
{
"address":"深圳市",
"age":24,
"flag":true,
"id":1,
"name":"兮动人"
},
{
"address":"北京市",
"age":25,
"flag":false,
"id":2,
"name":"Mike"
}
]
1.3 序列化Map集合
@Test
public void mapToJson(){
Student student = new Student();
student.setId(1);
student.setName("兮动人");
student.setAge(24);
student.setAddress("深圳市");
student.setFlag(true);
Student student2 = new Student();
student2.setId(2);
student2.setName("Mike");
student2.setAge(25);
student2.setAddress("北京市");
student2.setFlag(false);
Map<String,Student> map = new HashMap<String, Student>();
map.put("s1",student);
map.put("s2",student2);
String jsonString = JSON.toJSONString(map);
System.out.println(jsonString);
}
{
"s1":{
"address":"深圳市",
"age":24,
"flag":true,
"id":1,
"name":"兮动人"
},
"s2":{
"address":"北京市",
"age":25,
"flag":false,
"id":2,
"name":"Mike"
}
}
2. FashJson反序列化API
- 反序列化:JSON 格式字符串,反序列化为 Java 对象
2.1 反序列化Java对象
- 方法:
JSON.parseObject
@Test
public void jsonToObject(){
String jsonString = "{\"address\":\"深圳市\",\"age\":24,\"flag\":true,\"id\":1,\"name\":\"兮动人\"}";
Student student = JSON.parseObject(jsonString, Student.class);
System.out.println(student);
}
Student(id=1, name=兮动人, age=24, address=深圳市, flag=true)
2.2 反序列化List集合
- 方法:
JSON.parseArray
@Test
public void jsonToList(){
String jsonString = "[{\"address\":\"深圳市\",\"age\":24,\"flag\":true,\"id\":1,\"name\":\"兮动人\"}," +
"{\"address\":\"北京市\",\"age\":25,\"flag\":false,\"id\":2,\"name\":\"Mike\"}]";
List<Student> list = JSON.parseArray(jsonString,Student.class);
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student);
}
}
Student(id=1, name=兮动人, age=24, address=深圳市, flag=true)
Student(id=2, name=Mike, age=25, address=北京市, flag=false)
2.3 反序列化Map集合
- 方法:
JSON.parseObject
@Test
public void jsonToMap(){
String jsonString = "{\"s1\":{\"address\":\"深圳市\",\"age\":24,\"flag\":true,\"id\":1,\"name\":\"兮动人\"}," +
"\"s2\":{\"address\":\"北京市\",\"age\":25,\"flag\":false,\"id\":2,\"name\":\"Mike\"}}";
Map<String,Student> parse = JSON.parseObject(jsonString,new TypeReference<Map<String,Student>>(){});
for(String s : parse.keySet()){
System.out.println(s + ":"+parse.get(s));
}
}
s1:Student(id=1, name=兮动人, age=24, address=深圳市, flag=true)
s2:Student(id=2, name=Mike, age=25, address=北京市, flag=false)
3. SerializerFeature枚举
- 该枚举支持序列化的一些特性数据定义
3.1 WriteMapNullValue
- 枚举常量
WriteMapNullValue
序列化为null
的字段
@Test
public void testSerializerFeature(){
Student student = new Student();
student.setId(1);
student.setName("兮动人");
student.setAge(24);
// student.setAddress("深圳市");
student.setFlag(true);
String jsonString = JSON.toJSONString(student, SerializerFeature.WriteMapNullValue);
System.out.println(jsonString);
}
{"address":null,"age":24,"flag":true,"id":1,"name":"兮动人"}
3.2 WriteNullStringAsEmpty
- 枚举常量
WriteNullStringAsEmpty
字段为null
,序列化为""
String jsonString = JSON.toJSONString(student, SerializerFeature.WriteNullStringAsEmpty);
{"address":"","age":24,"flag":true,"id":1,"name":"兮动人"}
3.3 WriteNullNumberAsZero
- 枚举常量
WriteNullNumberAsZero
字段为null
,序列化为0
@Test
public void testAsZero(){
Student student = new Student();
student.setId(1);
student.setName("兮动人");
// student.setAge(24);
student.setAddress("深圳市");
student.setFlag(true);
String jsonString = JSON.toJSONString(student, SerializerFeature.WriteNullNumberAsZero);
System.out.println(jsonString);
}
{"address":"深圳市","age":0,"flag":true,"id":1,"name":"兮动人"}
3.4 WriteNullBooleanAsFalse
- 枚举常量
WriteNullBooleanAsFalse
字段值为null
输出false
@Test
public void testAsFalse(){
Student student = new Student();
student.setId(1);
student.setName("兮动人");
student.setAge(24);
student.setAddress("深圳市");
// student.setFlag(true);
String jsonString = JSON.toJSONString(student, SerializerFeature.WriteNullBooleanAsFalse);
System.out.println(jsonString);
}
3.5 WriteDateUseDateFormat
-
枚举常量
WriteDateUseDateFormat
格式化日期格式- 没有加属性的情况下,序列化话,日期显示的是毫秒值
@Test
public void testDateFormat(){
Student student = new Student();
student.setId(1);
student.setName("兮动人");
student.setAge(24);
student.setAddress("深圳市");
student.setFlag(true);
student.setBirthDate(new Date());
String jsonString = JSON.toJSONString(student);
System.out.println(jsonString);
}
- 加上格式化日期后:
String jsonString = JSON.toJSONString(student, SerializerFeature.WriteDateUseDateFormat);
3.6 PrettyFormat
- 也可以在后面都加上枚举常量
PrettyFormat
格式化输出,一目了然
String jsonString = JSON.toJSONString(student,SerializerFeature.WriteDateUseDateFormat,
SerializerFeature.PrettyFormat);
4. JSonField 注解
该注解作用于方法上,字段上和参数上,可在序列化和反序列化时进行特性功能定制。
1、 注解属性 : name
序列化后的名字
@Data
public class Student {
private Integer id;
@JSONField(name = "studentName")
private String name;
private Integer age;
private String address;
private boolean flag;
private Date birthDate;
}
@Test
public void testName() {
Student student = new Student();
student.setId(1);
student.setName("兮动人");
student.setAge(24);
student.setAddress("深圳市");
student.setFlag(true);
student.setBirthDate(new Date());
String jsonString = JSON.toJSONString(student);
System.out.println(jsonString);
}
2、注解属性 : ordinal
序列化后字段的顺序,属性值越小,顺序越靠前
- 可以看到上面序列化话默认的顺序是按照
a-z
字幕顺序来排序的 - 给字段设置顺序的属性值
@JSONField(name = "studentName", ordinal = 1)
private String name;
@JSONField(ordinal = 2)
private Integer age;
- 发现 值越小,就越靠前
3、注解属性 : format
指定序列化后的格式
@JSONField(format = "YYYY-MM-dd")
private Date birthDate;
4、注解属性 : serialize
指定是否序列化该字段,默认为 true
@JSONField(serialize = false)
private String address;
- 可以发现并没有序列化 address 字段
5、注解属性 : deserialize
指定是否反序列化该字段,默认为 true
6、注解属性 : serialzeFeatures
指定序列化时的特性定义
- 这个属性的用法和上面的 SerializerFeature枚举 用法是一致的
5. JSonType 注解
该注解作用于类上,对该类的字段进行序列化和反序列化时的特性功能定制。
1、注解属性 : includes 要被序列化的字段
@Data
@JSONType(includes = {"id", "name", "age"})
public class Person {
private int id;
private String name;
private int age;
private String address;
}
@Test
public void test() {
Person person = new Person();
person.setId(1);
person.setName("兮动人");
person.setAge(23);
person.setAddress("北京市");
String s = JSON.toJSONString(person);
System.out.println(s);
}
- 可以看到并没有序列化 address 字段、
2、注解属性 : orders 序列化后的顺序
@Data
@JSONType(includes = {"id", "name", "age", "address"}, orders = {"name", "age", "address", "id"})
public class Person {
private int id;
private String name;
private int age;
private String address;
}
@Test
public void test() {
Person person = new Person();
person.setId(1);
person.setName("兮动人");
person.setAge(23);
person.setAddress("北京市");
String s = JSON.toJSONString(person);
System.out.println(s);
}
3、注解属性 : serialzeFeatures 序列化时的特性定义
- serialzeFeatures 实现和上面案例上的枚举效果是一样的
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)