您的位置:首页 > Web前端 > JavaScript

JSON对象过滤不需要的属性

2013-08-05 00:00 302 查看
在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;

/**
* <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过滤属性