如何将多个 Map 解包为 JSON。示例
public class Class {
Map<String,String> firstMap;
Map<String,String> secondMap;
}
我只能对一个 Map 字段使用 @JsonAnyGetter。我还知道我可以使用自定义序列化器,但我在类中有更多字段,我不想更改反序列化方法。 首选 JSON 输出:
{
"Name": "Name",
"LastName": "LastName",
"firstMapKey": "firstMapValue"
"secondMapKey": "secondMapValue"
}
而不是:
{
"Name": "Name",
"LastName": "LastName",
"firstMap": {
"firstMapKey": "firstMapValue"
},
"secondMap": {
"secondMapKey": "secondMapValue"
}
}
最佳答案
@JonK 帮助了我:我添加了一张额外的 map 并将两张 map 合并到其中。对于附加 map ,我对用于合并的两个 map 使用了 @JsonAnyGetter 和 @JsonIgnore。
public class Class {
@JsonIgnore
Map<String,String> firstMap;
@JsonIgnore
Map<String,String> secondMap;
Map<String,String> compositeMap
@JsonAnyGetter
public Map<String, String> getCompositeMap() {
return compositeMap;
}
@JsonAnySetter
public void setCompositeMap(Map<String, String> compositeMap) {
this.compositeMap = compositeMap;
}
}
关于java - 如何将多个 Map<String,String> 解包为 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60225505/