1. 程式人生 > >Jackson 解析json的一些用法

Jackson 解析json的一些用法

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。