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();
相关推荐
WebVincent 2020-07-21
fengchao000 2020-07-04
baijinswpu 2020-06-28
88483063 2020-06-28
fengchao000 2020-06-16
somebodyoneday 2020-06-15
wujiajax 2020-06-14
somebodyoneday 2020-05-16
xx0cw 2020-05-16
newthon 2020-05-14
sailxu00 2020-04-27
fengchao000 2020-04-20
88483063 2020-01-29
83163452 2020-01-28
baijinswpu 2020-01-25
baijinswpu 2020-01-18