<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.47version>
dependency>
使用的实体类和统一结果类在文章末尾
@Test
void contextLoads() {
User user = new User();
String[] hobbies = {"sing","dance","rap"};
user.setId(1L);
user.setUsername("kunkun");
user.setAge(22);
user.setHobbies(hobbies);
user.setRegisterDate(new Date());
user.setBirthday(LocalDateTime.now());
String toJSONString = JSON.toJSONString(user);
System.out.println(toJSONString);
}
最后输出结果为:
{“age”:22,“birthday”:1723649279811,“hobbies”:[“sing”,“dance”,“rap”],“registerDate”:1723649279811,“username”:“kunkun”}
//对于对象中包含null值时
String toJSONString = JSON.toJSONString(user, SerializerFeature.WriteMapNullValue);
//对实体类修改
//fastjson序列化日期格式
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date registerDate;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime birthday;
@Test
void Test$Ref() {
ArrayList<User> userList = new ArrayList<>();
User user = new User();
user.setId(1L);
user.setUsername("zhangsan");
user.setAge(22);
userList.add(user);
userList.add(user);
userList.add(user);
//直接打印输出结果为:[{"age":22,"id":1,"username":"zhangsan"},{"$ref":"$[0]"},{"$ref":"$[0]"}]
//String json = JSON.toJSONString(userList);
//禁用引用探测功能
String json = JSON.toJSONString(userList,SerializerFeature.DisableCircularReferenceDetect);
//会发现结果变为正常
System.out.println(json);
}
对于SerializerFilter定制处理,等用到再学
@Test
void FastJsonSerializerFilter(){
String json = "{\"age\":22,\"birthday\":\"2024-08-14 23:36:47\",\"hobbies\":[\"sing\",\"dance\",\"rap\"],\"id\":null,\"registerDate\":\"2024-08-14 23:36:47\",\"username\":\"kunkun\"}";
//反序列化为user对象
User user = JSON.parseObject(json, User.class);
//User(id=null, username=kunkun, age=22, hobbies=[sing, dance, rap], registerDate=Wed Aug 14 23:36:47 CST 2024, birthday=2024-08-14T23:36:47)
System.out.println(user);
}
String resultJson = "{\"code\":200,\"data\":{\"age\":22,\"birthday\":\"2024-08-14 23:56:34\",\"hobbies\":[\"sing\",\"dance\",\"rap\"],\"id\":null,\"registerDate\":\"2024-08-14 23:56:34\",\"username\":\"kunkun\"},\"msg\":\"null\"}\n";
Result result = JSON.parseObject(resultJson, Result.class);
//再进行反序列化时,默认只能返回Object类型,需要进行强制类型转换
Object data = result.getData();
//可以使用TypeReference
Result<User> o = JSON.parseObject(resultJson, new TypeReference<Result<User>>() {
});
User data1 = o.getData();
写上在序列化或反序列化时都会生效
//指定属性名和json字符串key的对应关系
@JSONField( name = "address")
private String addr;
//不参与序列化,与反序列化
@JSONField( serialize = fales , deserialize = false)
private String password;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.Date;
@Data
public class User {
private Long id;
private String username;
private int age;
private String[] hobbies;
//fastjson序列化日期格式
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date registerDate;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime birthday;
}
package com.sst.result;
import lombok.Data;
@Data
public class Result<T> {
private int code;
private String msg;
private T data;
public Result(int code, String msg, T t) {
this.code = code;
this.msg = msg;
this.data = t;
}
}