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

How to extract particular field value from the json api response using fasterxml.jackson

我的一个 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;
    }
}

当我尝试在 "email":{} 下获取 msg 时,它返回 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 库的新手,不确定我缺少什么。

要获得 msg 值,我必须对上述模型 class 进行哪些更改。非常感谢您。

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

@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
}

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

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

终于可以访问您的 msg 了:

errData.getEmail().getMsg();

希望这已经足够清楚了。祝你好运!