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());
    }

}




你可能感兴趣的:(java)