JSON如何过滤对象属性

方法一:

    JsonConfig jsonConfig=new JsonConfig(); 

    config.setJsonPropertyFilter(new PropertyFilter(){
    public boolean apply(Object source, String name, Object value) {
     if(name.equals("familyMemberRoles") || name.equals("familyMemberRels")
       || name.equals("familyMemberRoles")
       || name.equals("familyBaseInfos") || name.equals("familyRoleAuthorities")
       || name.equals("memberIncomes") || name.equals("memberLoanouts")
       || name.equals("memberOnloans") || name.equals("memberOutlaies")
       || name.equals("memberRoleRels")) {          
      return true;
     } else {
      return false;
     }
    }
   });  


   FamilyBaseInfo familyBaseInfo = familyBaseInfoDao.findById(familyId);
   MemberBaseInfo memberBaseInfo = familyBaseInfo.getMemberBaseInfo();
   HashMap hashMap = new HashMap();
   hashMap.put("familyBaseInfo", familyBaseInfo);
   hashMap.put("memberBaseInfo", memberBaseInfo);
   JSONObject jSONObject = JSONObject.fromObject(familyBaseInfo, jsonConfig);
   return jSONObject.toString();

方法二:

   JsonConfig jsonConfig=new JsonConfig();  

   jsonConfig.setExcludes(new String[]{ "handler", "hibernateLazyInitializer",

   "familyMemberRoles", "familyMemberRels", "familyMemberRoles", "familyBaseInfos",

   "familyRoleAuthorities", "memberIncomes","memberLoanouts", "memberOnloans",

   "memberOutlaies", "memberRoleRels"});
   
   FamilyBaseInfo familyBaseInfo = familyBaseInfoDao.findById(familyId);
   MemberBaseInfo memberBaseInfo = familyBaseInfo.getMemberBaseInfo();
   HashMap hashMap = new HashMap();
   hashMap.put("familyBaseInfo", familyBaseInfo);
   hashMap.put("memberBaseInfo", memberBaseInfo);
   JSONObject jSONObject = JSONObject.fromObject(familyBaseInfo, jsonConfig);
   return jSONObject.toString();

   转自:http://blog.sina.com.cn/s/blog_8909899b0100wcbn.html

相关推荐