直接操作
1.引入fastjson依赖包
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
2.创建对象(对象上的注解先不用管后面解释)
@JSONType(includes = {"id","name","address","date","flag"})
public class Student {
@JSONField(name = "id1")
private int id;
private String name;
private String address;
private Date date;
private boolean flag;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String adddress) {
this.address = adddress;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", adddress='" + address + '\'' +
", date=" + date +
", flag=" + flag +
'}';
}
}
3.序列号(将对象转化成json字符串)和反序列化代码(将json字符串转化成对象)
//对象转化为json字符串
Student student = new Student();
student.setId(1);
student.setName("tmm");
student.setDate(new Date());
student.setFlag(false);
String s = JSON.toJSONString(student,SerializerFeature.WriteMapNullValue);
System.out.println(s);
结果:
{"address":null,"date":1648726711219,"flag":false,"id":1,"name":"tmm"}
SerializerFeature属性百度查看,WriteMapNullValue 是否输出值为null的字段,默认为false,这样null值就会被序列号出来
Student student2 = new Student();
student2.setId(2);
student2.setName("jyq");
student2.setDate(new Date());
student2.setFlag(true);
student2.setAddress("world");
//map转化为json字符串
Map<String,Student> map = new HashMap<String, Student>();
map.put("1",student);
map.put("2",student2);
String s1 = JSON.toJSONString(map);
System.out.println(s1);
结果:
{"1":{"date":1648727246049,"flag":false,"id":1,"name":"tmm"},"2":{"address":"world","date":1648727246154,"flag":true,"id":2,"name":"jyq"}}
//list转化为json字符串
List<Student> list = new ArrayList<Student>();
list.add(student);
list.add(student2);
String s2 = JSON.toJSONString(list);
System.out.println(s2);
结果:
[{"date":1648727317834,"flag":false,"id":1,"name":"tmm"},{"address":"world","date":1648727317931,"flag":true,"id":2,"name":"jyq"}]
//将json字符串转化成对象
String ss = "{\"date\":1648724323890,\"flag\":false,\"id\":1,\"name\":\"tmm\"}";
Student student1 = JSON.parseObject(ss, Student.class);
System.out.println(student1);
结果:
Student{id=1, name='tmm', adddress='null', date=Thu Mar 31 18:58:43 CST 2022, flag=false}
//将json字符串转化成list集合
String ss1 = "[{\"date\":1648724537601,\"flag\":false,\"id\":1,\"name\":\"tmm\"},{\"adddress\":\"world\",\"date\":1648724537700,\"flag\":true,\"id\":2,\"name\":\"jyq\"}]";
List<Student> student12 = JSON.parseArray(ss1, Student.class);
for (Student student3 : student12) {
System.out.println(student3);
}
结果:
Student{id=1, name='tmm', adddress='null', date=Thu Mar 31 19:02:17 CST 2022, flag=false}
Student{id=2, name='jyq', adddress='null', date=Thu Mar 31 19:02:17 CST 2022, flag=true}
//将json字符串转化成map集合
String ss3 = "{\"1\":{\"date\":1648724699626,\"flag\":false,\"id\":1,\"name\":\"tmm\"},\"2\":{\"adddress\":\"world\",\"date\":1648724699721,\"flag\":true,\"id\":2,\"name\":\"jyq\"}}";
Map<String,Student> map1 = JSON.parseObject(ss3, new TypeReference<Map<String,Student>>(){});
for (String key: map1.keySet()) {
System.out.println(key+"::"+map1.get(key));
}
结果:
1::Student{id=1, name='tmm', adddress='null', date=Thu Mar 31 19:04:59 CST 2022, flag=false}
2::Student{id=2, name='jyq', adddress='null', date=Thu Mar 31 19:04:59 CST 2022, flag=true}
注解理解:
@JSONType(includes = {"id","name","address","date","flag"})
public class Student {
@JSONField(name = "id1")
private int id;
private String name;
private String address;
private Date date;
......
@JSONField中:
修改和json字符串的字段映射【name】
格式化数据【format】
过滤掉不需要序列化的字段【serialize】
@JSONType中:
orders() //字段序列化顺序
includes() //包含的序列化字段
ignores() //不包含的序列化字段