欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

fastjson,处理空字段问题。

程序员文章站 2022-03-15 23:25:24
...

 

       在使用fastjson传递数据过程中,项目需求保留第一级空节点,去除非第一级空节点。

 

      以下,是我根据项目的实际场景,自己简化的一个场景。 

 

      项目代码结构如下所示:

fastjson,处理空字段问题。

 

 

      1、基本的JavaBean文件

 

      1.1、 ModelJson.java文件 


package com.fastJson.model2;

import java.util.List;

public class ModelJson {
	
	private List<School> schoolDetails;
	
	private List<OfferMsg> offerMsgDetails;
	
	
	
	
	@Override
	public String toString() {
		return "ModelJson [schoolDetails=" + schoolDetails + ", offerMsgDetails=" + offerMsgDetails + "]";
	}

	public ModelJson(List<School> schoolDetails, List<OfferMsg> offerMsgDetails) {
		super();
		this.schoolDetails = schoolDetails;
		this.offerMsgDetails = offerMsgDetails;
	}

	public ModelJson() {
		super();
	}
	
	
	
	
	public List<School> getSchoolDetails() {
		return schoolDetails;
	}

	public void setSchoolDetails(List<School> schoolDetails) {
		this.schoolDetails = schoolDetails;
	}

	public List<OfferMsg> getOfferMsgDetails() {
		return offerMsgDetails;
	}

	public void setOfferMsgDetails(List<OfferMsg> offerMsgDetails) {
		this.offerMsgDetails = offerMsgDetails;
	}
	
}



 

      1.2、OfferAttr.java文件


package com.fastJson.model2;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

public class OfferAttr {
	
	private Long id;
	
	private String offerName;
	
	private Integer offerNum;
	
	// @JSONField(format = "yyyy-MM-dd HH:mm:ss")
	private Date createDate;
	
	// @JSONField(format = "yyyy-MM-dd HH:mm:ss")
	private Date modifyDate;
	
	
	
	
	@Override
	public String toString() {
		return "OfferAttr [id=" + id + ", offerName=" + offerName + ", offerNum=" + offerNum + ", createDate="
				+ createDate + ", modifyDate=" + modifyDate + "]";
	}

	public OfferAttr(Long id, String offerName, Integer offerNum, Date createDate, Date modifyDate) {
		super();
		this.id = id;
		this.offerName = offerName;
		this.offerNum = offerNum;
		this.createDate = createDate;
		this.modifyDate = modifyDate;
	}

	public OfferAttr() {
		super();
	}
	
	
	
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getOfferName() {
		return offerName;
	}

	public void setOfferName(String offerName) {
		this.offerName = offerName;
	}

	public Integer getOfferNum() {
		return offerNum;
	}

	public void setOfferNum(Integer offerNum) {
		this.offerNum = offerNum;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getModifyDate() {
		return modifyDate;
	}

	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}
	
}



 

      1.3、OfferMsg.java文件


package com.fastJson.model2;

import java.util.Date;
import java.util.List;

public class OfferMsg {
	
	private String message;
	
	private Integer number;
	
	// @JSONField(format = "yyyy-MM-dd HH:mm:ss")
	private Date createDate;
	
	// @JSONField(format = "yyyy-MM-dd HH:mm:ss")
	private Date modifyDate;
	
	private List<OfferAttr> offerAttrList;
	
	
	
	
	@Override
	public String toString() {
		return "OfferMsg [message=" + message + ", number=" + number + ", createDate=" + createDate + ", modifyDate="
				+ modifyDate + ", offerAttrList=" + offerAttrList + "]";
	}

	public OfferMsg(String message, Integer number, Date createDate, Date modifyDate, List<OfferAttr> offerAttrList) {
		super();
		this.message = message;
		this.number = number;
		this.createDate = createDate;
		this.modifyDate = modifyDate;
		this.offerAttrList = offerAttrList;
	}
	
	public OfferMsg() {
		super();
	}
	
	
	
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Integer getNumber() {
		return number;
	}

	public void setNumber(Integer number) {
		this.number = number;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getModifyDate() {
		return modifyDate;
	}

	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}

	public List<OfferAttr> getOfferAttrList() {
		return offerAttrList;
	}

	public void setOfferAttrList(List<OfferAttr> offerAttrList) {
		this.offerAttrList = offerAttrList;
	}
	
}



 

      1.4、School.java文件 


package com.fastJson.model2;

import java.util.Date;
import java.util.List;

public class School {
	
	private String name;
	
	private String address;
	
	private Integer age;
	
	// @JSONField(format = "yyyy-MM-dd HH:mm:ss")
	private Date createDate;
	
	// @JSONField(format = "yyyy-MM-dd HH:mm:ss")
	private Date modifyDate;
	
	private List<Student> studentList;
	
	private List<Teacher> teacherList;
	
	
	
	
	@Override
	public String toString() {
		return "School [name=" + name + ", address=" + address + ", age=" + age + ", createDate=" + createDate
				+ ", modifyDate=" + modifyDate + ", studentList=" + studentList + ", teacherList=" + teacherList + "]";
	}

	public School(String name, String address, Integer age, Date createDate, Date modifyDate, List<Student> studentList,
			List<Teacher> teacherList) {
		super();
		this.name = name;
		this.address = address;
		this.age = age;
		this.createDate = createDate;
		this.modifyDate = modifyDate;
		this.studentList = studentList;
		this.teacherList = teacherList;
	}
	
	public School() {
		super();
	}
	
	
	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
	
	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}
	
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getModifyDate() {
		return modifyDate;
	}

	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}

	public List<Student> getStudentList() {
		return studentList;
	}

	public void setStudentList(List<Student> studentList) {
		this.studentList = studentList;
	}

	public List<Teacher> getTeacherList() {
		return teacherList;
	}

	public void setTeacherList(List<Teacher> teacherList) {
		this.teacherList = teacherList;
	}
	
}



 

      1.5、Student.java文件


package com.fastJson.model2;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

public class Student {
	
	private String name;
	
	private Integer age;
	
	// @JSONField(format = "yyyy-MM-dd HH:mm:ss")
	private Date createDate;
	
	// @JSONField(format = "yyyy-MM-dd HH:mm:ss")
	private Date modifyDate;
	
	private Long score;
	
	
	

	public Student(String name, Integer age, Date createDate, Date modifyDate, Long score) {
		super();
		this.name = name;
		this.age = age;
		this.createDate = createDate;
		this.modifyDate = modifyDate;
		this.score = score;
	}

	public Student(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}

	public Student() {
		super();
	}
	
	
	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	
	public Date getModifyDate() {
		return modifyDate;
	}

	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}

	public Long getScore() {
		return score;
	}

	public void setScore(Long score) {
		this.score = score;
	}
	
	
}



 

      1.6、Teacher.java文件


package com.fastJson.model2;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

public class Teacher {
	
	private String name;
	
	private Integer sex;  // 1-男, 2-女.
	
	// @JSONField(format = "yyyy-MM-dd HH:mm:ss")
	private Date createDate;
	
	// @JSONField(format = "yyyy-MM-dd HH:mm:ss")
	private Date modifyDate;
	
	
	

	@Override
	public String toString() {
		return "Teacher [name=" + name + ", sex=" + sex + ", createDate=" + createDate + ", modifyDate=" + modifyDate
				+ "]";
	}

	public Teacher(String name, Integer sex, Date createDate, Date modifyDate) {
		super();
		this.name = name;
		this.sex = sex;
		this.createDate = createDate;
		this.modifyDate = modifyDate;
	}

	public Teacher(String name, Integer sex) {
		super();
		this.name = name;
		this.sex = sex;
	}

	public Teacher() {
		super();
	}
	
	
	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getSex() {
		return sex;
	}

	public void setSex(Integer sex) {
		this.sex = sex;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getModifyDate() {
		return modifyDate;
	}

	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}
	
}



 

      1.7、TestModel2Main.java文件


package com.fastJson;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fastJson.model2.ModelJson;
import com.fastJson.model2.School;
import com.fastJson.model2.Student;
import com.fastJson.model2.Teacher;

public class TestModel2Main {
	
	public static void main(String[] args) {
		
		ModelJson modelJson = new ModelJson();
		
		List<School> schoolDetails = new ArrayList<School>();
		
		List<Student> studentList = new ArrayList<Student>();
		List<Teacher> teacherList = new ArrayList<Teacher>();
		
		Student student = new Student("张三", 19, new Date(), new Date(), 80L);
		Teacher teacher = new Teacher("马士兵", 45);
		Teacher teacher2 = new Teacher("高晓云", 40, new Date(), new Date());
		
		studentList.add(student);
		teacherList.add(teacher);
		teacherList.add(teacher2);
		
		School school = new School("北京新梦想学校", "北京市海淀区西三旗", 12, new Date(), new Date(), studentList, teacherList);
		
		schoolDetails.add(school);
		modelJson.setSchoolDetails(schoolDetails);
		
		String result = handleModelJson(modelJson);
		String result2 = handleModelJson2(modelJson);
		
		System.out.println("handleModelJson()函数处理结果为: " + result);
		System.out.println("-------------------------------------------------------");
		System.out.println("handleModelJson2()函数处理结果为: " + result2);
		
	}
	
	
	/**
	 * 说明:这种处理方式,当new Date()数据类型字段为空时,无法将其自动过滤掉.
	 * 使用的fastjson版本为:fastjson-1.2.47.jar     
	 * 
	 * @param modelJson
	 * @return
	 * 
	 * @author moon  2019/02/17  17:36  
	 */
	public static String handleModelJson(ModelJson modelJson) {
		
		return JSON.toJSONString(modelJson, SerializerFeature.WriteNullListAsEmpty);
	}
	
	
	/**
	 * 说明:这种处理方式,当new Date()数据类型字段为空时,可将其自动过滤掉.
	 * 使用的fastjson版本为:fastjson-1.2.47.jar     
	 * 
	 * @param modelJson
	 * @return
	 * 
	 * @author moon  2019/02/17  17:38  
	 */
	public static String handleModelJson2(ModelJson modelJson) {
		
		return JSON.toJSONString(JSON.parse(JSON.toJSONString(modelJson, SerializerFeature.WriteNullListAsEmpty)));
	}
	
	
	
}




 

      2、程序运行结果

 

      2.1、handleModelJson(...)函数运行结果如下,当new Date()数据类型的字段值为null时,其不可自动过滤。

 

fastjson,处理空字段问题。

 

 

      2.2、handleModelJson2(...)函数运行结果如下,当new Date()数据类型的字段值为null时,可自动过滤。

 

fastjson,处理空字段问题。