FastJson 是阿里巴巴的开源JSON解析库,它可以解析 JSON 格式的字符串,支持将 Java Bean 序列化为 JSON 字符串,也可以从 JSON 字符串反序列化到 JavaBean
Fastjson 的优点
序列化 : 是指将Java对象转成json格式字符串的过程.JavaBean对象,List集合对象,Map集合,为应用最广泛的
@Test
public void objectToJson(){
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setAddress("北京市");
String jsonString = JSON.toJSONString(student);
System.out.println(jsonString);
}
结果:
{"address":"北京市","age":20,"id":1,"name":"张三"}
@Test
public void listToJson(){
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setAddress("北京市");
Student student2 = new Student();
student2.setId(2);
student2.setName("李四");
student2.setAge(22);
student2.setAddress("天津市");
List<Student> list = new ArrayList<>();
list.add(student);
list.add(student2);
String jsonString = JSON.toJSONString(list);
System.out.println(jsonString);
}
结果:
[{"address":"北京市","age":20,"id":1,"name":"张三"},{"address":"天津市","age":22,"id":2,"name":"李四"}]
@Test
public void mapToJson(){
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setAddress("北京市");
Student student2 = new Student();
student2.setId(2);
student2.setName("李四");
student2.setAge(22);
student2.setAddress("天津市");
Map<String,Student> map = new HashMap<>();
map.put("s1",student);
map.put("s2",student2);
String jsonString = JSON.toJSONString(map);
System.out.println(jsonString);
}
结果:
{"s1":{"address":"北京市","age":20,"id":1,"name":"张三"},"s2":{"address":"天津市","age":22,"id":2,"name":"李四"}}
@Test
public void jsonToObject(){
String jsonString = "{\"address\":\"北京市\",\"age\":20,\"id\":1,\"name\":\"张三\"}";
Student student = JSON.parseObject(jsonString, Student.class);
System.out.println(student);
}
结果:
Student(id=1, name=张三, age=20, address=北京市)
@Test
public void jsonToList(){
String jsonString = "[{\"address\":\"北京市\",\"age\":20,\"id\":1,\"name\":\"张三\"},{\"address\":\"天津市\",\"age\":22,\"id\":2,\"name\":\"李四\"}]";
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=20, address=北京市)
Student(id=2, name=李四, age=22, address=天津市)
@Test
public void jsonToMap(){
String jsonString = "{\"s1\":{\"address\":\"北京市\",\"age\":20,\"id\":1,\"name\":\"张三\"},\"s2\":{\"address\":\"天津市\",\"age\":22,\"id\":2,\"name\":\"李四\"}}";
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=20, address=北京市)
s2:::Student(id=2, name=李四, age=22, address=天津市)
该枚举支持序列化的一些特性数据定义
@Test
public void testDefault(){
Student student = new Student();
student.setId(1);
student.setName(null);
student.setAge(20);
//student.setAddress("北京市");
student.setDate(new Date());
String jsonString = JSON.toJSONString(student);
System.out.println(jsonString);
}
结果:
{"age":20,"date":1684245428959,"id":1}
@Test
public void testSerializerFeature(){
Student student = new Student();
student.setId(1);
student.setName(null);
student.setAge(20);
//student.setAddress("北京市");
String jsonString = JSON.toJSONString(student, SerializerFeature.WriteMapNullValue);
System.out.println(jsonString);
}
结果:
{"address":null,"age":20,"id":1,"name":null}
@Test
public void testSerializerFeature(){
Student student = new Student();
student.setId(1);
student.setName(null);
student.setAge(20);
//student.setAddress("北京市");
String jsonString = JSON.toJSONString(student, SerializerFeature.WriteMapNullValue);
System.out.println(jsonString);
}
结果:
{"address":"","age":20,"id":1,"name":""}
@Test
public void testSerializerFeature() {
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setDate(new Date());
String jsonString = JSON.toJSONString(student,
SerializerFeature.WriteDateUseDateFormat,
SerializerFeature.PrettyFormat);
System.out.println(jsonString);
}
结果:
{
"age":20,
"date":"2023-05-16 21:54:20",
"id":1,
"name":"张三"
}
该注解作用于方法上,字段上和参数上.可在序列化和反序列化时进行特性功能定制
@Data
public class Student {
@JSONField(ordinal = 2)
private Integer id;
@JSONField(name = "studentName")
private String name;
@JSONField(ordinal = 1)
private Integer age;
@JSONField(serialize = false)
private String address;
@JSONField(format = "YYYY-MM-dd")
private Date date;
}
@Test
public void test1() {
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setAddress("北京市");
student.setDate(new Date());
String jsonString = JSON.toJSONString(student);
System.out.println(jsonString);
}
结果:
{"date":"2023-05-16","studentName":"张三","age":20,"id":1}
该注解作用于类上,对该类的字段进行序列化和反序列化时的特性功能定制.