是否可以对 jsonproperty 注释使用正则表达式以及如何完成?
我有来自第三方的 json,其属性前缀如下:
{
"1. information": "testing"
"2. information": "testing 2"
}
我希望所有信息都附加到列表数组信息,请参阅下面的伪示例:
public class TimeSeries {
@JsonProperty(/.*information/g)
private List<String> information;
}
有时,某些字符串属性也可以用日期作为后缀,这是可能的,但除此之外还有唯一的名称。这里可以使用正则表达式吗?
{
"Price 2019-09-01": "testing"
"Quantity 2019-09-01": "testing 2"
}
最佳答案
我不知道是否可以将正则表达式放入@JsonProperty中。
但是您可以将 @JsonAnySetter 用于“信息”字段。
Map<String, Object> informations = new LinkedHashMap<>();
@JsonAnySetter
void setInformation(String key, Object value) {
informations.put(key, value);
}
基本上,您映射具有静态名称的所有字段:
@JsonProperty("name")
String name;
并在信息中放置 json 中的所有其他字段。
关于java - 如何在 Jackson 的注释 JSON 属性中使用正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58341181/