在fastjson中使用SimplePropertyPreFilter忽略指定属性

在实际得开发过程中,我们经常会遇到以下场景,我们后端请求某个接口后获取到得数据,不希望将所有字段都返回给前端,那么我们需要封装,或者过滤一些不必要得字段后返回给前端,那么我们这里介绍下SimplePropertyPreFilter是如何实现的。

举个例子:
我们现在有以下json串:

"{\"name\":\"jhon\",\"age\":18,\"sex\":\"男\",\"phone\":\"1111111\",\"email\":\"142qq.com\90@qq.com\"}"

现在有以下几种方式进行过滤:

场景一: 只保留name字段

 public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "jhon");
        jsonObject.put("age", 18);
        jsonObject.put("sex", "男");
        jsonObject.put("phone", "1111111");
        jsonObject.put("email","142qq.com");
        System.out.println(jsonObject);
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
        filter.getIncludes().add("name");
        System.out.println(JSONObject.toJSONString(jsonObject, filter));

    }

运行结果如下:

{"phone":"1111111","sex":"男","name":"jhon","age":18,"email":"142qq.com"}
{"name":"jhon"}

场景二: 过滤掉name字段

 public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "jhon");
        jsonObject.put("age", 18);
        jsonObject.put("sex", "男");
        jsonObject.put("phone", "1111111");
        jsonObject.put("email","142qq.com");
        System.out.println(jsonObject);
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
//        filter.getIncludes().add("name");
        filter.getExcludes().add("name");
        System.out.println(JSONObject.toJSONString(jsonObject, filter));

    }

运行结果如下:

{"phone":"1111111","sex":"男","name":"jhon","age":18,"email":"142qq.com"}
{"phone":"1111111","sex":"男","age":18,"email":"142qq.com"}

场景三: 过滤掉email和phone字段

  public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "jhon");
        jsonObject.put("age", 18);
        jsonObject.put("sex", "男");
        jsonObject.put("phone", "1111111");
        jsonObject.put("email","142qq.com");
        System.out.println(jsonObject);
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
//        filter.getIncludes().add("name");
        filter.getExcludes().add("email");
        filter.getExcludes().add("phone");
        System.out.println(JSONObject.toJSONString(jsonObject, filter));

    }

运行结果如下:

{"phone":"1111111","sex":"男","name":"jhon","age":18,"email":"142qq.com"}
{"sex":"男","name":"jhon","age":18}

场景四: 只保留name和sex字段

  public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "jhon");
        jsonObject.put("age", 18);
        jsonObject.put("sex", "男");
        jsonObject.put("phone", "1111111");
        jsonObject.put("email","142qq.com");
        System.out.println(jsonObject);
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter("name","sex");
//        filter.getIncludes().add("name");
//        filter.getExcludes().add("email");
//        filter.getExcludes().add("phone");
        System.out.println(JSONObject.toJSONString(jsonObject, filter));

    }

或者:

 public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "jhon");
        jsonObject.put("age", 18);
        jsonObject.put("sex", "男");
        jsonObject.put("phone", "1111111");
        jsonObject.put("email","142qq.com");
        System.out.println(jsonObject);
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
//        filter.getIncludes().add("name");
        filter.getIncludes().add("email");
        filter.getIncludes().add("phone");
        System.out.println(JSONObject.toJSONString(jsonObject, filter));

    }

运行结果如下:

{"phone":"1111111","sex":"男","name":"jhon","age":18,"email":"142qq.com"}
{"phone":"1111111","email":"142qq.com"}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初夏0811

你的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值