对象转json忽略特定属性

一、使用jaskson包

1.maven配置:

        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.5
        

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字符串。
     * 

Title: pojoToJson

*

Description:

* @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 jsonToPojo(String jsonData, Class beanType) { try { T t = MAPPER.readValue(jsonData, beanType); return t; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 将json数据转换成pojo对象list *

Title: jsonToList

*

Description:

* @param jsonData * @param beanType * @return */ public static List jsonToList(String jsonData, Class beanType) { JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType); try { List list = MAPPER.readValue(jsonData, javaType); return list; } catch (Exception e) { e.printStackTrace(); } return null; } }

第二种方式:使用json-lib包

1.maven配置

        
        
            net.sf.json-lib
            json-lib
            2.4
            jdk15
        

2.  使用JsonConfig指定要忽略的属性

public String findByTypeCode() throws IOException {
        //调用业务层查询
        List 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;
    }

原文:https://www.cnblogs.com/ya-qiang/p/9364435.html

你可能感兴趣的:(json)