json:剔除多余的属性的写法

一.背景:

 根据查询结果,剔除多余的属性,结果:

PageBean [currentPage=1, totalPage=4, pageSize=3, totalNum=12, list=[Order [orderNum=1, receiverInfo=sd, price=888.00, customer=Customer [id=3, cusName=张liu, cusImgsrc=null, cusPhone=1231sdf23]], Order [orderNum=10, receiverInfo=sf, price=788.00, customer=Customer [id=3, cusName=张liu, cusImgsrc=null, cusPhone=1231sdf23]], Order [orderNum=11, receiverInfo=dg, price=788.00, customer=Customer [id=3, cusName=张liu, cusImgsrc=null, cusPhone=1231sdf23]]]]二.
二. 使用的包:
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
三.代码
PropertyFilter filter = new PropertyFilter() {
			@Override
			public boolean apply(Object arg0, String fieldName, Object arg2) {
				if ("cusPhone".equalsIgnoreCase(fieldName)) {
					return false;
				}
				if ("id".equalsIgnoreCase(fieldName)) {
					return false;
				}
				if ("orders".equalsIgnoreCase(fieldName)) {
					return false;
				}

				return true;
			}
		};
		String json = JSONArray.toJSONString(pageBean, filter, SerializerFeature.DisableCircularReferenceDetect);
		System.out.println(json);
三.结果:
json:{"currentPage":1,"list":[{"customer":{"cusName":"张liu"},"orderNum":"1","price":888.00,"receiverInfo":"sd"},{"customer":{"cusName":"张liu"},"orderNum":"10","price":788.00,"receiverInfo":"sf"},{"customer":{"cusName":"张liu"},"orderNum":"11","price":788.00,"receiverInfo":"dg"}],"pageSize":3,"totalNum":12,"totalPage":4}




你可能感兴趣的:(代码)