博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSON对象过滤不需要的属性
阅读量:7304 次
发布时间:2019-06-30

本文共 3858 字,大约阅读时间需要 12 分钟。

hot3.png

在WEB项目的接口编程中,通常要去掉不需要的属性字段,以降低流量。总结了一下网上的常用的办法,这里是自定义一个JSON对象过滤类,请看代码:

其中所需的jar包:

package test;import java.lang.reflect.Field;import java.util.Collection;import java.util.Set;import net.sf.json.JSONObject;import net.sf.json.JsonConfig;import net.sf.json.util.PropertyFilter;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory; /** * 

Title: 忽略属性

*

Description:忽略JAVABEAN的指定属性、是否忽略集合类属性

* */public class IgnoreFieldProcessorImpl implements PropertyFilter { Log log = LogFactory.getLog(this.getClass()); /** * 忽略的属性名称 */ private String[] fields; /** * 是否忽略集合 */ private boolean ignoreColl = false; /** * 空参构造方法
* 默认不忽略集合 */ public IgnoreFieldProcessorImpl() { // empty } /** * 构造方法 * @param fields 忽略属性名称数组 */ public IgnoreFieldProcessorImpl(String[] fields) { this.fields = fields; } /** * 构造方法 * @param ignoreColl 是否忽略集合 * @param fields 忽略属性名称数组 */ public IgnoreFieldProcessorImpl(boolean ignoreColl, String[] fields) { this.fields = fields; this.ignoreColl = ignoreColl; } /** * 构造方法 * @param ignoreColl 是否忽略集合 */ public IgnoreFieldProcessorImpl(boolean ignoreColl) { this.ignoreColl = ignoreColl; } public boolean apply(Object source, String name, Object value) { Field declaredField = null; //忽略值为null的属性 if(value == null) return true; //剔除自定义属性,获取属性声明类型 if(!"data".equals(name) && "data"!=name && !"totalSize".equals(name) && "totalSize"!=name ){ try { declaredField = source.getClass().getDeclaredField(name); } catch (NoSuchFieldException e) { log.equals("没有找到属性" + name); e.printStackTrace(); } } // 忽略集合 if (declaredField != null) { if(ignoreColl) { if(declaredField.getType() == Collection.class || declaredField.getType() == Set.class) { return true; } } } // 忽略设定的属性 if(fields != null && fields.length > 0) { if(juge(fields,name)) { return true; } else { return false; } } return false; } /** * 过滤忽略的属性 * @param s * @param s2 * @return */ public boolean juge(String[] s,String s2){ boolean b = false; for(String sl : s){ if(s2.equals(sl)){ b=true; } } return b; } public String[] getFields() { return fields; } /** * 设置忽略的属性 * @param fields */ public void setFields(String[] fields) { this.fields = fields; } public boolean isIgnoreColl() { return ignoreColl; } /** * 设置是否忽略集合类 * @param ignoreColl */ public void setIgnoreColl(boolean ignoreColl) { this.ignoreColl = ignoreColl; } public static void main(String[]args){ JsonConfig config = new JsonConfig(); config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(true, new String[]{"name"})); // 忽略掉name属性及集合对象 Entity entity = new Entity(); entity.setAddress("xxxxxxx"); entity.setAge(20); entity.setName("lxb"); JSONObject fromObject = JSONObject.fromObject(entity, config ); System.out.print(fromObject.toString()); }}实体类对象代码:package test;public class Entity {    private Long id;    private String name;    private String sex;    private String address;    private Integer age;    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }        }测试结果为:{"address":"xxxxxxx","age":20},可以发现其中name属性已经被过滤了。

转载于:https://my.oschina.net/u/232879/blog/150285

你可能感兴趣的文章
C#使用Xamarin开发可移植移动应用进阶篇(6.使用渲染器针对单个平台自定义控件..很很很很重要..),附源码...
查看>>
实验二
查看>>
简单安装ubuntu
查看>>
20160331javaweb 之JSP page 指令
查看>>
用Ruby批量获取电影的评分与影片信息
查看>>
2019.5.29 区块链论文翻译
查看>>
Centos6.6安装mysql记录
查看>>
OCP读书笔记(5) - 使用RMAN创建备份
查看>>
java的接口和抽象类区别
查看>>
能够提高PHP的性能的一些注意事项
查看>>
020-请你说一说app测试的工具
查看>>
软件测试2019:第五次作业—— 安全测试(含安全测试工具实验)
查看>>
SSM框架搭建总结(2)
查看>>
Python学习(19)正则表达式
查看>>
PHP中空字符串、0、null、empty和false之间的关系
查看>>
【深度学习篇】---CNN和RNN结合与对比,实例讲解
查看>>
201771010126 王燕《面向对象程序设计(Java)》第十二周学习总结
查看>>
XAML实例教程系列 - 资源(Resources)
查看>>
LWIP互联网资料汇总
查看>>
外贸术语
查看>>