json转化的时候如何忽略某些属性字段值⼀、有时候在将对象或list对象转化为json的时候,我们可能不需要所有的属性值,这就需要我们去过滤掉这些属性了 我下⾯说两种⽐较流⾏的json包如何来忽略某些属性值
⼆、使⽤jaskson包
1. maven配置:
<!-- Jackson Json处理⼯具包 -->
<dependency>
<groupId>com.</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;
json转换对象
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.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 = adValue(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 = TypeFactory().constructParametricType(List.class, beanType);
try {
List<T> list = adValue(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.Dict_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打印到页⾯
return NONE;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论