Jackson 解析json的一些用法

本文介绍了Jackson库在处理JSON解析时的几种用法:1) 如何让Jackson在序列化枚举时只显示特定字段的值;2) 如何在对象转换为JSON时忽略属性为null的字段;3) 如何自定义字段的显示名称。通过示例和分析,展示了Jackson的@JsonValue、@JsonSerialize和@JsonProperty注解的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、问题:如何让Jackson 对枚举中中特定字段进行解析?

经常我们用枚举来表示某组相关属性值。

举个栗子:

/**
 * 性别类型
 */
public enum GenderType {
    GENDER_UNKNOWN(0, "未知"),
    GENDER_MALE(1, "男性"),
    GENDER_FEMAL(2, "女性"),
    GENDER_OTHER(3, "其他"),
    ;
    private int value;
    private String comment;
    private GenderType(int value, String comment){
        this.value = value;
        this.comment = comment;
    }

    @JsonValue
    public int getValue() {
        return value;
    }

    public String getComment() {
        return comment;
    }

}

这里,我们希望Jackson解析GenderType时,只将对应类型的value解析出来,通过在属性的getValue()方法上加@JsonValue,这样设置的枚举值在通过Jackson解析时,会自动将枚举替换成对应的value值。

举个栗子:

public class Student {

    private String name;

    private GenderType genderType;


    public String getName() {
        return name;
    }

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

    public GenderType getGenderType() {
        return genderType;
    }

    public void setGenderType(GenderType genderType) {
        this.genderType = genderType;
    }

    public static void main(String[] args)  throws  Exception{
        Student student = new Student();
        student.setName("tim");
        student.setGenderType(GenderType.GENDER_FEMAL);
        System.out.println(JsonUtils.writeObject2Json(student));
    }
}

结果输出:

{"name":"tim","genderType":2}

分析:上面我们将Student的类的性别字段设置为枚举类型,在通过Jackson解析成json数据时,因为设置了@JsonValue因此会解析成我们需要的值。


2、问题:如何让Jackson 当转换对象为json时,如果属性为null时,不输出?

通过在实体前加入:@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)

举个栗子:

Student.java 类:
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Student {

    private String name;
    private String sex;

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }
;
    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }

    public static void main(String[] args) throws JsonProcessingException {
        Student student = new Student();
        student.setName("tim");
        System.out.println(JsonUtils.writeObject2Json(student));
    }
}

输出:

{"name":"tim"}

分析:

上述通过加入@JsonSerialize(include =JsonSerialize.Inclusion.NON_NULL)注解,这样当我们的示例student虽然只设置name字段值为”tim”,此时,sex字段的值为null,但是Jackson通过上述注解忽略了字段值为null。


3、问题:更改字段的显示值?

通过加入注解:@JsonProperty(“”)

举个栗子:

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Student {

    @JsonProperty("error_code")
    private int errorCode;
    private String name;
    private String sex;

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }
;
    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }

    public static void main(String[] args) throws JsonProcessingException {
        Student student = new Student();
        student.setErrorCode(0);
        student.setName("tim");
        System.out.println(JsonUtils.writeObject2Json(student));
    }
}

输出:

{"name":"tim","error_code":0}

分析:

通过加入@JsonProperty("error_code")注解,能将之前字段名为errorCode,显示的时候改为error_code。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值