如何使用fasterxml.jackson从json api响应中提取特定字段值

时间:2018-02-10 05:54:42

标签: json jackson gson fasterxml

我的一个api回复如下 -

{
   "statusCode": 422,
   "error": "Unprocessable Entity",
   "message": "Bad data received",
   "err_data": {
       "email": {
           "location": "body",
           "param": "email",
           "value": false,
           "msg": "Please provide valid e-mail address"
       }
   }
}

因此,下面的response.asString()代表上述回复正文。

ApiResponse response = new Gson().fromJson(response.asString(), ApiResponse.class);

ApiResponse.class是我的模型,如下所示:

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "statusCode", "message" })
public class ApiResponse implements Serializable {

    private static final long serialVersionUID = 1L;

    @JsonProperty("message")
    private String message;

    @JsonProperty("statusCode")
    private int statusCode;

    @JsonProperty("err_data")
    private List<String> errData = new ArrayList<>();

    @JsonProperty("email")
    private List<String> email = new ArrayList<>();

    @JsonProperty("msg")
    private String msg;

    /**
     * No args constructor for use in serialization
     */
    public ApiResponse() {
    }

    /**
     * @param message
     */
    public ApiResponse(int statusCode, String message, List<String> errData, List<String> email, String msg) {
        this.message = message;
        this.statusCode = statusCode;
        this.errData = errData;
        this.email = email;
        this.msg = msg;
    }

    @JsonProperty("statusCode")
    public int getStatusCode() {
        return statusCode;
    }

    @JsonProperty("statusCode")
    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    @JsonProperty("message")
    public String getMessage() {
        return message;
    }

    @JsonProperty("message")
    public void setMessage(String message) {
        this.message = message;
    }

    @JsonProperty("err_data")
    public List<String> getErrData() {
        return errData;
    }

    @JsonProperty("err_data")
    public void setErrData(List<String> errData) {
        this.errData = errData;
    }

    @JsonProperty("email")
    public List<String> getEmail() {
        return email;
    }

    @JsonProperty("email")
    public void setEmail(List<String> email) {
        this.email = email;
    }

    @JsonProperty("msg")
    public String getMsg() {
        return msg;
    }

    @JsonProperty("msg")
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

当我试图在msg下获取"email":{}时,它会返回null

ApiResponse apiResponse = new Gson().fromJson(response.asString(), ApiResponse.class);
// this prints correct value
System.out.println(apiResponse.getMessage()); 

// this prints correct value
System.out.println(apiResponse.getStatusCode());

// this prints empty string array => []
System.out.println(apiResponse.getErrData());

// this also prints empty string array => []
System.out.println(apiResponse.getEmail());

// this prints null
System.out.println(apiResponse.getMsg());

我是fasterxml.jackson lib的新手,不知道我错过了什么。

要获得msg值,我必须在上面的模型类中进行哪些更改。非常感谢你提前。

1 个答案:

答案 0 :(得分:2)

这是您的代码不正确的地方:

@JsonProperty("err_data")
private List<String> errData = new ArrayList<>();

@JsonProperty("email")
private List<String> email = new ArrayList<>();

emailerrData都不是List,它们是单独的Object。就像ApiResponse.java一样,您需要为两个对象创建POJO。例如:

public class Email {
     private String location;
     private String param;
     private String value;
     private String msg;
     // define getter and setter
}

public class ErrData {
     private Email email;
     // define getter and setter
}

然后使用新类作为对象类型。

@JsonProperty("err_data")
private ErrData errData; 
// email is already inside ErrData, you don't need to define them here

最后访问您的msg

errData.getEmail().getMsg();

希望这很清楚。祝你好运!