json - 当对象名称中包含DOT时如何从json对象中检索字段值

标签 json jackson rest-assured fasterxml

我想使用fasterxml.jackson从json下面提取“msg”值-有人可以建议我我的模型类看起来如何吗?

{
    "statusCode": 422,
    "error": "Unprocessable Entity",
    "message": "Bad data received",
    "err_data": {
        "payment_details.type": {
            "location": "body",
            "param": "payment_details.type",
            "msg": "Must be either etransfer or cheque"
        }
    }
}

这是我所做的,但始终返回“null”!
@JsonInclude(JsonInclude.Include.ALWAYS)
public class MyApiResponse extends ParentResponse implements Serializable {
    private static final long serialVersionUID = 1L;

    @JsonProperty("payment_details")
    private PaymentDetails payment_details;

    @JsonProperty("payment_details")
    public PaymentDetails getPayment_details() {
        return payment_details;
    }

    @JsonProperty("payment_details")
    public void setPayment_details(PaymentDetails payment_details) {
        this.payment_details = payment_details;
    }
}
ParentResponse模型类扩展了ErrorResponse模型类,这就是它的样子。

ErrorResponse模型表示上述JSON。
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ErrorResponse implements Serializable {

    private static final long serialVersionUID = 1L;

    @JsonProperty("statusCode")
    private int statusCode;

    @JsonProperty("error")
    private String error;

    @JsonProperty("message")
    private String message;

    @JsonProperty("err_data")
    private ErrData err_data;

    @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("error")
    public String getError() {
        return error;
    }

    @JsonProperty("error")
    public void setError(String error) {
        this.error = error;
    }

    @JsonProperty("err_data")
    public ErrData getErr_data() {
        return err_data;
    }

    @JsonProperty("err_data")
    public void setErr_data(ErrData err_data) {
        this.err_data = err_data;
    }
}
err_data对象由以下模型类表示。
@JsonInclude(JsonInclude.Include.ALWAYS)
public class ErrData implements Serializable {

    private static final long serialVersionUID = 1L;

    @JsonProperty("email")
    private Email email;

    @JsonProperty("payment_details.type")
    private PaymentDetailsType payment_details_type;

    @JsonProperty("email")
    public Email getEmail() {
        return email;
    }

    @JsonProperty("email")
    public void setEmail(Email email) {
        this.email = email;
    }

    @JsonProperty("payment_details.type")
    public PaymentDetailsType getPayment_details_type() {
        return payment_details_type;
    }

    @JsonProperty("payment_details.type")
    public void setPayment_details_type(PaymentDetailsType payment_details_type) {
        this.payment_details_type = payment_details_type;
    }
}

下面的类表示的payment_details.type对象。
@JsonInclude(JsonInclude.Include.ALWAYS)
public class PaymentDetailsType extends ErrorMessage implements Serializable {

    private static final long serialVersionUID = 1L;

}

@JsonInclude(JsonInclude.Include.ALWAYS)
public class Email extends ErrorMessage implements Serializable {

    private static final long serialVersionUID = 1L;

}

最后是由ErrorMessage扩展的PaymentDetailsType,如下所示。
@JsonPropertyOrder({"location", "param", "value", "msg"})
public class ErrorMessage implements Serializable {

    private static final long serialVersionUID = 1L;

    @JsonProperty("location")
    private String location;

    @JsonProperty("param")
    private String param;

    @JsonProperty("value")
    private String value;

    @JsonProperty("msg")
    private String msg;

    @JsonProperty("location")
    public String getLocation() {
        return location;
    }

    @JsonProperty("location")
    public void setLocation(String location) {
        this.location = location;
    }

    @JsonProperty("param")
    public String getParam() {
        return param;
    }

    @JsonProperty("param")
    public void setParam(String param) {
        this.param = param;
    }

    @JsonProperty("value")
    public String getValue() {
        return value;
    }

    @JsonProperty("value")
    public void setValue(String value) {
        this.value = value;
    }

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

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

最后,我尝试获取“msg”字段值,如下所示-
new Gson().fromJson(response.asString(), MyApiResponse.class).getErr_data().getPayment_details_type().getMsg();

我认为这有一个问题-如果json中的字段名称为,则不确定如何定义getter方法。 (点)。
@JsonProperty("payment_details.type")
public PaymentDetailsType getPayment_details_type() {
    return payment_details_type;
}

与上面类似,我正在json下面进行操作以检索“msg”值,并且工作正常。
{
    "statusCode": 422,
    "error": "Unprocessable Entity",
    "message": "Bad data received",
    "err_data": {
        "email": {
            "location": "body",
            "param": "email",
            "value": "test @ com",
            "msg": "Must be a valid email"
        }
    }
}

这将返回正确的“msg”值。
new Gson().fromJson(response.asString(), MyApiResponse.class).getErr_data().getEmail().getMsg();

请提出建议!

谢谢。

最佳答案

这是一个最小的示例,显示了如何使用Jackson解析JSON,其中属性名称可能包含点:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;

class Main {
    public static void main(String[] args) throws IOException {
        String json = "{" +
                      "    \"payment_details.type\": {" +
                      "        \"location\": \"body\"" +
                      "    }" +
                      "}";
        ObjectMapper mapper = new ObjectMapper();
        Response response = mapper.readValue(json, Response.class);
        System.out.println(response.getPaymentDetailsType().getLocation());
    }
}

class Response {
    @JsonProperty("payment_details.type")
    private PaymentDetailsType paymentDetailsType;

    public PaymentDetailsType getPaymentDetailsType() { return paymentDetailsType; }
}

class PaymentDetailsType {
    private String location;

    public String getLocation() { return location; }
}

请注意,仅当无法从设置器或变量名称中推断出JSON中的预期属性名称时,才需要@JsonProperty

关于json - 当对象名称中包含DOT时如何从json对象中检索字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48940985/

相关文章:

java - 在 Java 中创建一个 JSON 对象并在 javascript/jquery 中访问它

iOS JSON 解析,数组与多个数组

json - Apache 约翰森 vs jackson

java - 如何使用 Jackson 的 ObjectMapper.readerForUpdating 忽略某些字段

java - 是否可以从响应对象中提取方法名称?

php - 将可变数量的参数传递到 PDO 语句并返回 JSON 对象

iphone - 使用嵌套 JSON 填充 UITableView

java - 使用 Jackson 将 Java 列表序列化为 XML 和 JSON

java - Rest Assured Framework 完成 JSON 响应匹配

java - 对 REST Assured 浮点比较的混淆