简体   繁体   English

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

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

I am working on mapping json values with kibana, one element ie amount it is returning beginning with "^" and its not mapping the getters and setters.我正在使用 kibana 映射 json 值,一个元素,即它以“^”开头的返回量,它不映射 getter 和 setter。 How could I map it?, I used @jsonProperty and @jsonCreator as well but still seeing null in the value.我怎么能 map 呢?我也使用了 @jsonProperty 和 @jsonCreator 但仍然在值中看到 null。

example:例子:

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

This is not (necessarily) an answer, but is needed to show code.这不是(必然)答案,但需要显示代码。

Unable to reproduce the issue posed in the question.无法重现问题中提出的问题。

The following Jackson mapping code using @JsonProperty works fine.以下使用@JsonProperty的 Jackson 映射代码工作正常。

Imports used below下面使用的导入

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

Option 1: Using @JsonProperty on fields选项 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 + "]";
    }
}

Option 2: Using @JsonProperty on methods选项 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 + "]";
    }
}

Option 3: Using @JsonCreator on constructor选项 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 + "]";
    }
}

Test测试

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

Output (same for all Foo classes above) Output(以上所有Foo类相同)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM