fastjson输出字段为Null的两种方式

1.@JSONField(serialzeFeatures= {SerializerFeature.WriteMapNullValue})

fastJson默认是不输出value为null的字段,如果在该字段上加上这个注解,则会输出

2.加过滤器,可以让value为null的字段都输出空字符串

public class User {

    private String name;

    private Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
   public static void main(String[] args) {
        User user = new User();
        user.setName("curry");

        ValueFilter filter = (Object object, String name, Object v) -> {
            if (v==null) {
                return "";
            }
            return v;
        };
        System.out.println(JSON.toJSONString(user, filter));
    }

输出:

{"age":"","name":"curry"}
posted @ 2019-06-27 10:51  Kobelieve  阅读(8028)  评论(0编辑  收藏  举报