关于FastJson大家一定都有了解,那么对于刚接触的朋友来说可能有点半懵半懂的感觉,那么我就来分享一下自己的理解,如果有不对的地方希望大家指出来,共同进步~
Fastjson 是一个 Java 库,可以将 Java 对象转换为 JSON 格式,当然它也可以将 JSON 字符串转换为 Java 对象。
Fastjson 可以操作任何 Java 对象,即使是一些预先存在的没有源码的对象。
Fastjson 源码地址:https://github.com/alibaba/fastjson
Fastjson 中文 Wiki:https://github.com/alibaba/fastjson/wiki/Quick-Start-CN
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>x.x.x</version>
</dependency>
其中 x.x.x 是版本号,根据需要使用特定版本,建议使用最新版本。
@Data
public class Student {
private Integer id;
private String name;
private Integer age;
private String email;
private Date birthday;
private String address;
}
//对象(可以是任何你能想到的格式,简单对象,集合,对象集合等)
Student student = new Student();
// Object --> JSONString
String jsonString = JSON.toJSONString(student);
// JSONString --> Object
Student p2 = JSON.parseObject(jsonString, Student.class);
//对象(可以是任何你能想到的格式,简单对象,集合,对象集合等)
List<Student> list = new ArrayList<>();
Student student1 = new Student();
list.add(student1);
// List --> JSONString
String JSONString = JSON.toJSONString(list);
// JSONString --> List
List<Student> studentList = JSON.parseArray(JSONString, Student.class);
Map<String, Student> map = new HashMap();
Student student1 = new Student();
map.put("student1", student1);
// Map --> JSONString
String JSONString = JSON.toJSONString(map);
// JSONString --> Map
Map<String, Student> studentMap = JSON.parseObject(JSONString, new TypeReference<Map<String, Student>>(){});
该枚举支持序列化的一些特性数据定义
@Test
public void testSerializerFeature() {
Student student = new Student();
student.setId(1);
student.setName("张三");
//student.setAge(20);
//student.setAddress("北京市");
student.setBirthday(new Date());
student.setEmail("zs@sina.com");
String jsonString1 = JSON.toJSONString(student);
System.out.println(jsonString1);
// {"birthday":1656215235138,"email":"zs@sina.com","id":1,"name":"张三"}
String jsonString2 = JSON.toJSONString(student, SerializerFeature.WriteMapNullValue);
System.out.println(jsonString2);
// {"address":null,"age":null,"birthday":1656215235138,"email":"zs@sina.com","id":1,"name":"张三"}
String jsonString3 = JSON.toJSONString(student, SerializerFeature.WriteNullStringAsEmpty);
System.out.println(jsonString3);
// {"address":"","birthday":1656215235138,"email":"zs@sina.com","id":1,"name":"张三"}
String jsonString4 = JSON.toJSONString(student, SerializerFeature.WriteNullNumberAsZero);
System.out.println(jsonString4);
// {"age":0,"birthday":1656215235138,"email":"zs@sina.com","id":1,"name":"张三"}
String jsonString5 = JSON.toJSONString(student, SerializerFeature.WriteNullBooleanAsFalse);
System.out.println(jsonString5);
// {"birthday":1656215235138,"email":"zs@sina.com","id":1,"name":"张三"}
String jsonString6 = JSON.toJSONString(student, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.PrettyFormat);
System.out.println(jsonString6);
// {
// "birthday":"2022-06-26 11:47:15",
// "email":"zs@sina.com",
// "id":1,
// "name":"张三"
// }
}
该注解作用于方法上,字段上和参数上.可在序列化和反序列化时进行特性功能定制.
该注解作用于类上,对该类的字段进行序列化和反序列化时的特性功能定制.
另附:菜鸟教程链接:https://www.runoob.com/w3cnote/fastjson-intro.html