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



>我正在努力使用 kibana 映射 json 值,一个元素,即它从"^"开始返回的数量,并且它不映射 getter 和 setter。我怎么能映射它?,我也使用了@jsonProperty和@jsonCreator,但仍然在值中看到 null。

例:

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

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

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

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

下面使用的进口

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 = "{rn" + 
"  "value": "530b8371",rn" + 
"  "Code": "AH",rn" + 
"  "^amount": "$275,817.49",rn" + 
"  "description": "grant"rn" + 
"}";
ObjectMapper objectMapper = new ObjectMapper();
Foo foo = objectMapper.readValue(json, Foo.class);
System.out.println(foo);

输出(上述所有Foo类相同(

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

最新更新