bean转json时null字段不转换的方法

1.使用GSON时,默认不对null字段进行转换,而ObjectMapper和JSONObject默认对null字段进行转换

(1)使用GSON时,对null字段进行转换的设置

 Gson gsonSerializeNull = new GsonBuilder().serializeNulls().create();

(2)使用ObjectMapper对null字段不进行转换的设置

ObjectMapper objectMapper= new ObjectMapper();
objectMapper.setSerializationInclusion(Include.NON_NULL);

(3)使用json-lib的JSONObject在bean转json时对null字段不转换的设置

 PropertyFilter filter = new PropertyFilter() {
            public boolean apply(Object object, String fieldName,
                    Object fieldValue) {
                return null == fieldValue;
            }
        };
        jsonConfig.setJsonPropertyFilter(filter);
        System.out.println(JSONObject.fromObject(s, jsonConfig).toString());

2.测试

Student类

package com.beanjson.test;


public class Student {
    private String name;

    private int id;

    private Integer age = null;

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}

main函数

package com.beanjson.test;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Test {

    private static ObjectMapper objectMapper;

    public static void main(String[] args) throws JsonProcessingException {
        Student s = new Student();
        s.setName("abc");
        s.setId(1);
        s.setAge(null);

        System.out.println("----------gson(默认对null不转换)---------");
        Gson gson = new Gson();
        System.out.println(gson.toJson(s));
        System.out.println("----------gson(对null字段转换)---------");
        Gson gsonSerializeNull = new GsonBuilder().serializeNulls().create();
        System.out.println(gsonSerializeNull.toJson(s));

        System.out.println("----------objectMapper对null字段不转换的设置---------");
        objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(Include.NON_NULL);
        System.out.println(objectMapper.writeValueAsString(s));

        System.out.println("----------json-lib对null字段不转换的设置---------");
        JsonConfig jsonConfig = new JsonConfig();
        PropertyFilter filter = new PropertyFilter() {
            public boolean apply(Object object, String fieldName,
                    Object fieldValue) {
                return null == fieldValue;
            }
        };
        jsonConfig.setJsonPropertyFilter(filter);
        System.out.println(JSONObject.fromObject(s, jsonConfig).toString());
    }

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值