SimplePropertyPreFilter的使用与详解(使用SimplePropertyPreFilter过滤JSON属性)

SimplePropertyPreFilter是fastjson下的过滤器
可以使用SimplePropertyPreFilter进行JSON的属性过滤 或者特定字段提取

SimplePropertyPreFilter的使用

 public static void main(String[] args) {
        TestBean bean = new TestBean("111", "222", "333", 1);
        //使用List<String>也可以
        ImmutableSet<String> build1 = ImmutableSet.<String>builder()
                .add("key1")
                .add("string1")
                .add("integer1")
                .build();
        SimplePropertyPreFilter simpFilter = new SimplePropertyPreFilter();
        //.getIncludes()--包括指定的字段
        simpFilter.getIncludes().addAll(build1);
        //.getExcludes()--不包括指定的字段
        //simpFilter.getExcludes().addAll(build1);
        String jsonStr = JSON.toJSONString(bean, simpFilter, SerializerFeature.WriteMapNullValue);
        System.out.println(jsonStr);    //{"integer1":1,"string1":"111"}
    }

    @Data
    public static class TestBean {
        private String string1;
        private String string2;
        private String string3;
        private Integer integer1;

        public TestBean(String string1, String string2, String string3, Integer integer1) {
            this.string1 = string1;
            this.string2 = string2;
            this.string3 = string3;
            this.integer1 = integer1;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值