在实际得开发过程中,我们经常会遇到以下场景,我们后端请求某个接口后获取到得数据,不希望将所有字段都返回给前端,那么我们需要封装,或者过滤一些不必要得字段后返回给前端,那么我们这里介绍下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"}