一、有时候在将对象或list对象转化为json的时候,我们可能不需要所有的属性值,这就需要我们去过滤掉这些属性了
我下面说两种比较流行的json包如何来忽略某些属性值
二、 使用jaskson包
1. maven配置:
<!-- Jackson Json处理工具包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency>
2. 在需要忽略字段属性上使用注解来配置:
//@JsonIgnoreProperties({"dict_id","dict_item_code"}) // 可以使用此注解放在类上忽略多个属性值public class BaseDict { @JsonIgnore //可以直接放在field上面表示要忽略的filed private String dict_id; private String dict_type_code; private String dict_type_name; private String dict_item_name; private String dict_item_code; private Integer dict_sort; private String dict_enable; private String dict_memo; public String getDict_id() { return dict_id; } public void setDict_id(String dict_id) { this.dict_id = dict_id; } public String getDict_type_code() { return dict_type_code; } public void setDict_type_code(String dict_type_code) { this.dict_type_code = dict_type_code; } public String getDict_type_name() { return dict_type_name; } public void setDict_type_name(String dict_type_name) { this.dict_type_name = dict_type_name; } public String getDict_item_name() { return dict_item_name; } public void setDict_item_name(String dict_item_name) { this.dict_item_name = dict_item_name; } public String getDict_item_code() { return dict_item_code; } public void setDict_item_code(String dict_item_code) { this.dict_item_code = dict_item_code; } public Integer getDict_sort() { return dict_sort; } public void setDict_sort(Integer dict_sort) { this.dict_sort = dict_sort; } public String getDict_enable() { return dict_enable; } public void setDict_enable(String dict_enable) { this.dict_enable = dict_enable; } public String getDict_memo() { return dict_memo; } public void setDict_memo(String dict_memo) { this.dict_memo = dict_memo; } }
3. 使用封装的jsonUtils来实现操作
package com.sshcrm.utils;import java.util.List;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.JavaType;import com.fasterxml.jackson.databind.ObjectMapper;/** * CRM自定义响应结构 */public class JsonUtils { // 定义jackson对象 private static final ObjectMapper MAPPER = new ObjectMapper(); /** * 将对象转换成json字符串。 * <p>Title: pojoToJson</p> * <p>Description: </p> * @param data * @return */ public static String objectToJson(Object data) { try { String string = MAPPER.writeValueAsString(data); return string; } catch (JsonProcessingException e) { e.printStackTrace(); } return null; } /** * 将json结果集转化为对象 * * @param jsonData json数据 * @param clazz 对象中的object类型 * @return */ public static <T> T jsonToPojo(String jsonData, Class<T> beanType) { try { T t = MAPPER.readValue(jsonData, beanType); return t; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 将json数据转换成pojo对象list * <p>Title: jsonToList</p> * <p>Description: </p> * @param jsonData * @param beanType * @return */ public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) { JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType); try { List<T> list = MAPPER.readValue(jsonData, javaType); return list; } catch (Exception e) { e.printStackTrace(); } return null; } }
三、使用json-lib包
1. maven配置
<!--json-lib依赖 --> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency>
2. 使用JsonConfig指定要忽略的属性
public String findByTypeCode() throws IOException { //调用业务层查询 List<BaseDict> result = baseDictService.findByTypeCode(baseDict.getDict_type_code()); //将list转化为json /** * JSONConfig: 转JSON的配置对象 * JSONAarray:将数组和list集合转成JSON * JSONObject: 将对象和Map集合转成JSON */ JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(new String[] {"dict_sort","dict_enable","dict_memo"}); JSONArray jsonArray = JSONArray.fromObject(result, jsonConfig); //将json打印到页面 ServletActionContext.getResponse().setContentType("text/html;charset=utf-8"); ServletActionContext.getResponse().getWriter().print(jsonArray.toString()); return NONE; }
原文出处:http://www.cnblogs.com/ya-qiang/p/9364435.html