JSON 在值中返回插入符 (^) 并在响应中返回 null

JSON returning caret (^) in the value and returning null in the response

我正在使用 kibana 映射 json 值,这是一个元素,即它以“^”开头返回的数量,它不映射 getter 和 setter。我如何映射它?我也使用了@json属性和@jsonCreator,但仍然在值中看到空值。

示例:

{
  "value": "530b8371",
  "Code": "AH",
  "^amount": "5,817.49",
  "description": "grant"
}

这不是(必然)答案,但需要显示代码。

无法重现问题中提出的问题。

以下使用 @JsonProperty 的 Jackson 映射代码工作正常。

以下使用进口

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

选项 1:在字段上使用 @JsonProperty

class Foo {
    @JsonProperty
    private String value;
    @JsonProperty("Code")
    private String code;
    @JsonProperty("^amount")
    private String amount;
    @JsonProperty
    private String description;

    @Override
    public String toString() {
        return "Foo[value=" + this.value + ", code=" + this.code +
                 ", amount=" + this.amount + ", description=" + this.description + "]";
    }
}

选项 2:在方法上使用 @JsonProperty

class Foo {
    private String value;
    private String code;
    private String amount;
    private String description;

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

    @JsonProperty("Code")
    public String getCode() {
        return this.code;
    }
    public void setCode(String code) {
        this.code = code;
    }

    @JsonProperty("^amount")
    public String getAmount() {
        return this.amount;
    }
    public void setAmount(String amount) {
        this.amount = amount;
    }

    @JsonProperty
    public String getDescription() {
        return this.description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "Foo[value=" + this.value + ", code=" + this.code +
                 ", amount=" + this.amount + ", description=" + this.description + "]";
    }
}

选项 3:在构造函数上使用 @JsonCreator

class Foo {
    private String value;
    private String code;
    private String amount;
    private String description;

    @JsonCreator
    public Foo(@JsonProperty("value") String value,
               @JsonProperty("Code") String code,
               @JsonProperty("^amount") String amount,
               @JsonProperty("description") String description) {
        this.value = value;
        this.code = code;
        this.amount = amount;
        this.description = description;
    }

    @Override
    public String toString() {
        return "Foo[value=" + this.value + ", code=" + this.code +
                 ", amount=" + this.amount + ", description=" + this.description + "]";
    }
}

测试

String json = "{\r\n" + 
              "  \"value\": \"530b8371\",\r\n" + 
              "  \"Code\": \"AH\",\r\n" + 
              "  \"^amount\": \"5,817.49\",\r\n" + 
              "  \"description\": \"grant\"\r\n" + 
              "}";
ObjectMapper objectMapper = new ObjectMapper();
Foo foo = objectMapper.readValue(json, Foo.class);
System.out.println(foo);

输出(以上所有Foo类都相同)

Foo[value=530b8371, code=AH, amount=5,817.49, description=grant]