【发布时间】:2022-01-22 19:25:57
【问题描述】:
API 正在返回这样的地图条目数组
[
{"key":"Key1","value":"Value1"},
{"key":"Key2","value":"Value2"},
{"key":"Key3","value":"Value3"},
....
]
我想直接反序列化成Map<String,String>
所以它会变成
Map<String,String> {Key1->Value1, Key2->Value2, Key3->Value3}
明显的解决方案是反序列化为List<ApiMapEntry>,但也许有更简单的方法?
我现在在做什么:
@JsonProperty
public void setMyTargetMap(List<ApiMapEntryWrapper> wrappers) {
this.myTargetMap= wrappers.stream().collect(Collectors.toMap(ApiMapEntryWrapper::getKey, ApiMapEntryWrapper::getValue));
}
class ApiMapEntryWrapper{
private String key;
private String value;
}
虽然我会在不需要ApiMapEntryWrapper 的情况下寻找类似(参考示例 json)的东西
@JsonMap(keyName="key",valueName="value")
private Map<String,Strin> myTargetMap
【问题讨论】: