JSON对象过滤不需要的属性
2013-08-05 00:00
302 查看
在WEB项目的接口编程中,通常要去掉不需要的属性字段,以降低流量。总结了一下网上的常用的办法,这里是自定义一个JSON对象过滤类,请看代码:
其中所需的jar包:
其中所需的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; /** * <p>Title: 忽略属性</p> * <p>Description:忽略JAVABEAN的指定属性、是否忽略集合类属性</p> * */ public class IgnoreFieldProcessorImpl implements PropertyFilter { Log log = LogFactory.getLog(this.getClass()); /** * 忽略的属性名称 */ private String[] fields; /** * 是否忽略集合 */ private boolean ignoreColl = false; /** * 空参构造方法<br/> * 默认不忽略集合 */ 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属性已经被过滤了。
相关文章推荐
- Json-lib转对象时过滤不需要的属性
- fastjson 过滤不需要序列化的属性
- jsonobject 转换对象时过滤属性
- 对象转JSONArray,JSONObject[包括对象中日期格式化,属性过滤]
- Springboot使用@JsonView过滤对象属性
- JSON过滤对象中属性名相同(搜索的问题及答案)
- fastjson 过滤掉不需要序列化的属性
- extjs list转json并过滤不需要的属性/JsonConfig
- ALiBABa fastjson去除JSON对象不需要的属性字段
- FastJson toString过滤指定的对象属性
- 使用JsonConfig中的setExcludes方法过滤不需要转换的属性
- FastJson toString过滤指定的对象属性
- Java对象转出json并过滤指定属性
- fastjson 过滤不需要序列化的属性
- ALiBABa fastjson去除JSON对象不需要的属性字段
- jQuery表单对象属性过滤
- ExtJs中grid的dataIndex为传回Json数据的一个对象的一个属性时处理方式
- ajax请求返回JSON对象 (无需转换直接可点出属性)
- easyUI datagrid 显示json内部关联对象属性
- 将实体类Bean转换为json对象的时候过滤null空值