JSONObject.toJSONString()包含或排除指定的属性

1 背景
在Java开发中,通常需要将一个实体对象转换成Json字符串,使用FastJson来实现这种转换十分方便,只要使用FastJson中JSONObject静态类提供的toJSONString()静态方法即可。但是在转换时,我们可能需要指定使用实体对象的某些属性来进行转换,或者指定转换时要排除实体对象的某些属性。

FastJson提供的SerializeFilter类就可以实现这种需求,可以指定转换时要包含的属性,或者指定转换时要排除的属性。

JSONObject.toJSONString()默认忽略值为null的属性这篇文章分析了JSONObject.toJSONString()将实体对象转换成JSON字符串时默认是忽略值为null的属性,以及如何设置包含值为null的属性。本文则主要是演示使用SerializeFilter来指定包含或者排除的属性,使得生成的JSON字符串中包含或者不包含某些属性。

2 演示程序
2.1 此程序演示了使用JSONObject提供的以下方法将实体对象转换成Json字符串:
public static String toJSONString(Object object, SerializeFilter filter, SerializerFeature... features);

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.PropertyPreFilters;

/**
 * 使用FastJson将实体对象转换成Json字符串测试类
 */
public class FastJsonApplication {

    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 = null;

        /**
         * 指定排除属性过滤器和包含属性过滤器
         * 指定排除属性过滤器:转换成JSON字符串时,排除哪些属性
         * 指定包含属性过滤器:转换成JSON字符串时,包含哪些属性
         */
        String[] excludeProperties = {"country", "city"};
        String[] includeProperties = {"id", "username", "mobile"};
        PropertyPreFilters filters = new PropertyPreFilters();
        PropertyPreFilters.MySimplePropertyPreFilter excludefilter = filters.addFilter();
        excludefilter.addExcludes(excludeProperties);
        PropertyPreFilters.MySimplePropertyPreFilter includefilter = filters.addFilter();
        includefilter.addIncludes(includeProperties);


        /**
         * 情况一:默认忽略值为null的属性
         */
        jsonUser = JSONObject.toJSONString(user, SerializerFeature.PrettyFormat);
        System.out.println("情况一:\n" + jsonUser);

        /**
         * 情况二:包含值为null的属性
         */
        jsonUser = JSONObject.toJSONString(user, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
        System.out.println("情况二:\n" + jsonUser);

        /**
         * 情况三:默认忽略值为null的属性,但是排除country和city这两个属性
         */
        jsonUser = JSONObject.toJSONString(user, excludefilter, SerializerFeature.PrettyFormat);
        System.out.println("情况三:\n" + jsonUser);

        /**
         * 情况四:包含值为null的属性,但是排除country和city这两个属性
         */
        jsonUser = JSONObject.toJSONString(user, excludefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
        System.out.println("情况四:\n" + jsonUser);

        /**
         * 情况五:默认忽略值为null的属性,但是包含id、username和mobile这三个属性
         */
        jsonUser = JSONObject.toJSONString(user, includefilter, SerializerFeature.PrettyFormat);
        System.out.println("情况五:\n" + jsonUser);

        /**
         * 情况六:包含值为null的属性,但是包含id、username和mobile这三个属性
         */
        jsonUser = JSONObject.toJSONString(user, includefilter, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue);
        System.out.println("情况六:\n" + jsonUser);
    }

    /**
     * 用户实体类
     */
    public static class User {
        private Long id;
        private String username;
        private String password;
        private String mobile;
        private String country;
        private String city;

        public Long getId() {
            return id;
        }

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

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getMobile() {
            return mobile;
        }

        public void setMobile(String mobile) {
            this.mobile = mobile;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }
    }

}
————————————————
3 结果说明
情况一和情况二说明了public static String toJSONString(Object object, SerializeFilter filter, SerializerFeature… features)这个方法将实体对象转换成JSON字符串时,默认是忽略掉值为null的属性,并且说明了如何使得转换后的JSON字符串包含值为null的属性。
情况三和情况四说明了如何使用SerializeFilter来排除指定属性,使得转换后的JSON字符串中不包含这些属性。
情况五和情况六说明了如何使用SerializeFilter来包含指定属性,使得转换后的JSON字符串中只包含这些属性。
————————————————
版权声明:本文为CSDN博主「JavaBigData1024」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44516305/article/details/88821815原文链接:https://blog.csdn.net/weixin_44516305/article/details/88821815

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值