fastjson使用场景学习
public static void main(String[] args) {
/** --------- 序列化 开始--------- */
Student s = new Student("student", "北京市");
String jsonString = JSON.toJSONString(s);// 序列化 转换为 JSON
System.out.println(jsonString);// {"sAddress":"北京市","sName":"student"}
/** --------- 序列化 结束 --------- */
/** --------- 反序列化 开始--------- */
Student student = (Student) JSON.parseObject(jsonString, Student.class);// 反序列化
// 转换为
// 对象BEAN
System.out.println(student.toString());// Student [sName=student,
// sAddress=北京市]
/** --------- 反序列化 结束--------- */
/** --------- 反序列化为集合 开始--------- */
List<Student> list = new ArrayList<Student>();
for (int i = 0; i <= 2; i++) {
Student tempS = new Student("student" + (i + 1), "北京市" + (i + 1));
list.add(tempS);
}
String listJson = JSON.toJSONString(list);// 反序列化为集合
List<Student> studentList = JSON.parseObject(listJson,
new TypeReference<List<Student>>() {
});
System.out.println(studentList);
// [Student [sName=student1, sAddress=北京市1], Student [sName=student2,
// sAddress=北京市2], Student [sName=student3, sAddress=北京市3]]
/** --------- 反序列化为集合 结束--------- */
/** --------- 将对象中的空值输出 开始--------- */
// 1:在fastjson中,默认不输出空值的
Student stu = new Student("student", null);
String jsonStrStu = JSON.toJSONString(stu);
System.out.println(jsonStrStu);// {"sName":"student"}
// 2:在fastjson中,如果需要输出空值
String jsonStrStuNull = JSON.toJSONString(stu,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullListAsEmpty);
System.out.println(jsonStrStuNull);// {"sAddress":null,"sName":"student"}
/** --------- 将对象中的空值输出 结束--------- */
/** --------- 将对象集合 开始--------- */
School school = new School();
school.setName("清华大学");
for (int i = 0; i <= 2; i++) {
Student tempS = new Student("student" + (i + 1), "北京市" + (i + 1));
school.addStuList(tempS);
}
String jsonStrSchool = JSON.toJSONString(school);//序列化
System.out.println("jsonStrSchool : " + jsonStrSchool);
//jsonStrSchool : {"name":"清华大学","stuList":[{"sAddress":"北京市1","sName":"student1"},
//{"sAddress":"北京市2","sName":"student2"},{"sAddress":"北京市3","sName":"student3"}]}
School schoolObject = JSON.parseObject(jsonStrSchool, School.class);//json转对象
System.out.println(schoolObject);//School [name=清华大学, stuList=[Student [sName=student1, sAddress=北京市1],
//Student [sName=student2, sAddress=北京市2], Student [sName=student3, sAddress=北京市3]]]
/** --------- 将对象集合 结束--------- */
/** --------- 处理日期 开始--------- */
String jsonStrDateFormat = JSON.toJSONStringWithDateFormat(new Date(),
"yyyy-MM-dd HH:mm:ss");
System.out.println(jsonStrDateFormat);// "2019-07-23 14:35:59"
/** --------- 处理日期 结束--------- */
}
Student.java
class Student {
String sName;
String sAddress;
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public String getsAddress() {
return sAddress;
}
public void setsAddress(String sAddress) {
this.sAddress = sAddress;
}
public Student() {
}
public Student(String sName, String sAddress) {
super();
this.sName = sName;
this.sAddress = sAddress;
}
@Override
public String toString() {
return "Student [sName=" + sName + ", sAddress=" + sAddress + "]";
}
}
School.java
class School {
String name;
List<Student> stuList = new ArrayList<Student>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addStuList(Student student) {
this.stuList.add(student);
}
public void setStuList(List<Student> stuList) {
this.stuList = stuList;
}
public List<Student> getStuList(){
return this.stuList;
}
public School(String name, List<Student> stuList) {
this.name = name;
this.stuList = stuList;
}
public School() {
}
@Override
public String toString() {
return "School [name=" + name + ", stuList=" + stuList + "]";
}
}