jsonConfig.setExcludes用法
/**
* 联系人实体
*/
public class LinkMan {
private Integer id; // 编号
private Customer customer; // 所属客户
private String linkName; // 姓名
private String sex; // 性别
private String zhiwei; // 职位
private String officePhone; // 办公电话
private String phone; // 手机
}
/**
* 客户实体
*/
public class Customer {
private Integer id; // 编号
private String khno; // 客户编号 动态生成
private String name; // 客户名称
private String area; // 客户地区
private String cusManager; // 客户经理
private String address; // 客户地址
其他属性略
}
目标
LinkMan对象中有Customer的属性对象,如果想不要这个可以使用jsonConfig工具中的excludes剔除
jsonConfig.setExcludes(new String[]{“customer”});
HashMap<String,Object> map = new HashMap<>();
map.put("cusId", cusId);
List<LinkMan> linkManList = linkManService.getLinkManList(map);
JSONObject result=new JSONObject();
JsonConfig jsonConfig=new JsonConfig();
jsonConfig.setExcludes(new String[]{"customer"});
//JSONArray jsonArray=JSONArray.fromObject(linkManList);//不剔除
JSONArray jsonArray=JSONArray.fromObject(linkManList,jsonConfig);//剔除customer属性
结果
不剔除customer属性的结果如下
[{"customer":{"address":"","area":"","cusManager":"","dsdjh":"","fax":"","fr":"","gsdjh":"","id":21,"khno":"KH20150526073027","khyh":"","khzh":"","level":"大客户","myd":"","name":"百度","nyye":"","phone":"","postCode":"","state":0,"webSite":"","xyd":"","yyzzzch":"","zczj":""},"id":8,"linkName":"22","officePhone":"1","phone":"1","sex":"男","zhiwei":"21"},{"customer":{"address":"","area":"","cusManager":"","dsdjh":"","fax":"","fr":"","gsdjh":"","id":21,"khno":"KH20150526073027","khyh":"","khzh":"","level":"大客户","myd":"","name":"百度","nyye":"","phone":"","postCode":"","state":0,"webSite":"","xyd":"","yyzzzch":"","zczj":""},"id":9,"linkName":"2244","officePhone":"24","phone":"4","sex":"男","zhiwei":"422"}]
剔除customer属性后的结果如下
[{"id":8,"linkName":"22","officePhone":"1","phone":"1","sex":"男","zhiwei":"21"},{"id":9,"linkName":"2244","officePhone":"24","phone":"4","sex":"男","zhiwei":"422"}]
注:用的是net下的包
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;