在WEB項目的接口編程中,一般要去掉不須要的屬性字段,以下降流量。總結了一下網上的經常使用的辦法,這裏是自定義一個JSON對象過濾類,請看代碼: java
其中所需的jar包: apache
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屬性已經被過濾了。