这里只讲JSON-LIB和FastJson两种Json工具的序列化过滤问题
比如,我想过滤的属性有id,和name,先演示json-lib
1.Json-Lib
//通过json-lib将数据序列化为json格式
JsonConfig jsonConfig = new JsonConfig();
//进行过滤
jsonConfig. setExcludes(new String[]{"id","name"});
//转化成json数据格式
JSONObject jsonObject = new JSONObject();
String json = jsonObject.toString();
//将json数据通过流写入到客户端
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
response.getWriter().print(json);
2.FastJson
//使用fastjson,过滤
SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
//过滤字段
比如,我想过滤的属性有id,和name,先演示json-lib
1.Json-Lib
//通过json-lib将数据序列化为json格式
JsonConfig jsonConfig = new JsonConfig();
//进行过滤
jsonConfig. setExcludes(new String[]{"id","name"});
//转化成json数据格式
JSONObject jsonObject = new JSONObject();
String json = jsonObject.toString();
//将json数据通过流写入到客户端
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");
response.getWriter().print(json);
2.FastJson
//使用fastjson,过滤
SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
//过滤字段