我正在尝试将地图转换为地图。我有一个 json 和 Object :{ A: { availableInfo: { isAvailable : true} }, VV: { availableInfo: { isAvailable : false} }, B45: { availableInfo: { isAvailable : null} }}和对象:@Builder@Getter@AllArgsConstructor@JsonIgnoreProperties(ignoreUnknown = true)public class AvailableInfo { @JsonProperty("isAvailable") private Boolean isAvailable; public AvailableInfo() { }}我试过了 :Map<String, AvailableInfo> response = getResponse(query, Map.class);但我收到错误:java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to AvailableInfo下面是 getResponse() 方法:private <T> T getResponse(final RestURI query, final Class<T> responseClass) throws IOException { T response = null; final RestResponse<Record> tempResponse = client.call(GET, query); if (isResponseOK(tempResponse, query)) { byte[] payloads = {}; for (Record rec : tempResponse.getResponses()) { final byte[] payload = rec.getPayload(); payloads = ArrayUtils.addAll(payloads, payload); } final Reader reader = newReader(payloads) response = jsonObjectMapper.readValue(reader, responseClass); } return response; //returns Map<String, LinkedHashMap> }知道如何解决吗?我尝试将输入 json 更改为: { A: { isAvailable : true} , VV: { isAvailable : false} , B45: { isAvailable : null} }但仍然无法正常工作。
3 回答
Helenr
TA贡献1780条经验 获得超4个赞
首先使用以下行转换它,然后您可以使用数据:
List<? extends Map<String, String>> resultList = new ArrayList<LinkedHashMap<String, String>>();
慕妹3242003
TA贡献1824条经验 获得超6个赞
要将 json 转换为 Map,json 必须采用以下形式:
{
A: { isAvailable : true} ,
VV: { isAvailable : false} ,
B45: { isAvailable : null}
}
之后以下代码返回Map<String, AvailableInfo>:
Map<String, AvailableInfo> readValue = mapper.readValue(json, Map.class);
的输出System.out.println(readValue);:
{A={isAvailable=true}, VV={isAvailable=false}, B45={isAvailable=null}}
添加回答
举报
0/150
提交
取消