一、简述
有的java对象字段很多,但是我们需要使用的可能只有其中一两个,或者是对象中有特殊属性,不方便转成string字符串,因此需要指定只需要某几个属性,或者指定排除某几个属性,使用fastJson实现,完全没有问题,
二、各种情况
先看看效果,效果好或者有您需要的,您在往下看:
user对象:
User{id=1, username=‘古力娜扎’, password=’’, mobile=‘null’, country=‘中国’, city=‘北京’}
——————————————————————————————————
情况一:默认忽略值为null的属性:
{
“city”:“北京”,
“country”:“中国”,
“id”:1,
“password”:"",
“username”:“古力娜扎”
}
jsonUser = JSONObject.toJSONString(user, SerializerFeature.PrettyFormat);
System.out.println("——————————————————————————————————");
System.out.println("情况一:默认忽略值为null的属性:\n" + jsonUser);
——————————————————————————————————
情况二:包含值为null的属性:
{
“city”:“北京”,
“country”:“中国”,
“id”:1,
“mobile”:null,
“password”:"",
“username”:“古力娜扎”
}
jsonUser = JSONObject.toJSONString(user, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
System.out.println("——————————————————————————————————");
System.out.println("情况二:包含值为null的属性:\n" + jsonUser);
——————————————————————————————————
情况三:默认忽略值为null的属性,但是排除country和city这两个属性:
{
“id”:1,
“password”:"",
“username”:“古力娜扎”
}
String[] excludeProperties = {"country", "city"};
PropertyPreFilters filters = new PropertyPreFilters();
PropertyPreFilters.MySimplePropertyPreFilter excludefilter = filters.addFilter();
// 指定排除属性过滤器:转换成JSON字符串时,排除哪些属性
excludefilter.addExcludes(excludeProperties);
jsonUser = JSONObject.toJSONString(user, excludefilter, SerializerFeature.PrettyFormat);
System.out.println("——————————————————————————————————");
System.out.println("情况三:默认忽略值为null的属性,但是排除country和city这两个属性:\n" + jsonUser);
——————————————————————————————————
情况四:包含值为null的属性,但是排除country和city这两个属性:
{
“id”:1,
“mobile”:null,
“password”:"",
“username”:“古力娜扎”
}
String[] excludeProperties = {"country", "city"};
PropertyPreFilters filters = new PropertyPreFilters();
PropertyPreFilters.MySimplePropertyPreFilter excludefilter = filters.addFilter();
// 指定排除属性过滤器:转换成JSON字符串时,排除哪些属性
excludefilter.addExcludes(excludeProperties);
jsonUser = JSONObject.toJSONString(user, excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
System.out.println("——————————————————————————————————");
System.out.println("情况四:包含值为null的属性,但是排除country和city这两个属性:\n" + jsonUser);
——————————————————————————————————
情况五:默认忽略值为null的属性,但是包含id、username和mobile这三个属性:
{
“id”:1,
“username”:“古力娜扎”
}
String[] includeProperties = {"id", "username", "mobile"};
PropertyPreFilters.MySimplePropertyPreFilter includefilter = filters.addFilter();
// 指定包含属性过滤器:转换成JSON字符串时,包含哪些属性
includefilter.addIncludes(includeProperties);
jsonUser = JSONObject.toJSONString(user, includefilter, SerializerFeature.PrettyFormat);
System.out.println("——————————————————————————————————");
System.out.println("情况五:默认忽略值为null的属性,但是包含id、username和mobile这三个属性:\n" + jsonUser);
——————————————————————————————————
情况六:包含值为null的属性,但是包含id、username和mobile这三个属性:
{
“id”:1,
“mobile”:null,
“username”:“古力娜扎”
}
String[] includeProperties = {"id", "username", "mobile"};
PropertyPreFilters.MySimplePropertyPreFilter includefilter = filters.addFilter();
// 指定包含属性过滤器:转换成JSON字符串时,包含哪些属性
includefilter.addIncludes(includeProperties);
jsonUser = JSONObject.toJSONString(user, includefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
System.out.println("——————————————————————————————————");
System.out.println("情况六:包含值为null的属性,但是包含id、username和mobile这三个属性:\n" + jsonUser);
三、完整代码
package ef.test;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.PropertyPreFilters;
public class TestFastJson {
public static void main(String[] args) {
User user = new User();
user.setId(1L);
user.setUsername("古力娜扎");
user.setPassword("");
user.setMobile(null);
user.setCountry("中国");
user.setCity("北京");
String jsonUser;
// 指定排除属性过滤器和包含属性过滤器
String[] excludeProperties = {"country", "city"};
String[] includeProperties = {"id", "username", "mobile"};
PropertyPreFilters filters = new PropertyPreFilters();
PropertyPreFilters.MySimplePropertyPreFilter excludefilter = filters.addFilter();
// 指定排除属性过滤器:转换成JSON字符串时,排除哪些属性
excludefilter.addExcludes(excludeProperties);
PropertyPreFilters.MySimplePropertyPreFilter includefilter = filters.addFilter();
// 指定包含属性过滤器:转换成JSON字符串时,包含哪些属性
includefilter.addIncludes(includeProperties);
System.out.println("user对象:\n" + user.toString());
jsonUser = JSONObject.toJSONString(user, SerializerFeature.PrettyFormat);
System.out.println("——————————————————————————————————");
System.out.println("情况一:默认忽略值为null的属性:\n" + jsonUser);
jsonUser = JSONObject.toJSONString(user, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
System.out.println("——————————————————————————————————");
System.out.println("情况二:包含值为null的属性:\n" + jsonUser);
jsonUser = JSONObject.toJSONString(user, excludefilter, SerializerFeature.PrettyFormat);
System.out.println("——————————————————————————————————");
System.out.println("情况三:默认忽略值为null的属性,但是排除country和city这两个属性:\n" + jsonUser);
jsonUser = JSONObject.toJSONString(user, excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
System.out.println("——————————————————————————————————");
System.out.println("情况四:包含值为null的属性,但是排除country和city这两个属性:\n" + jsonUser);
jsonUser = JSONObject.toJSONString(user, includefilter, SerializerFeature.PrettyFormat);
System.out.println("——————————————————————————————————");
System.out.println("情况五:默认忽略值为null的属性,但是包含id、username和mobile这三个属性:\n" + jsonUser);
jsonUser = JSONObject.toJSONString(user, includefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
System.out.println("——————————————————————————————————");
System.out.println("情况六:包含值为null的属性,但是包含id、username和mobile这三个属性:\n" + jsonUser);
}
}