如何在Java中将LinkedHashMap转换为Map <string,object =“”>?

时间:2018-08-22 04:38:42

标签: java json jackson

我正在尝试将地图转换为地图。 我有一个json和Object:

UNMATCH = {**unmatch_1, **unmatch_2}

对象:

{
        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);

下面是getResponse()方法:

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to AvailableInfo

有什么想法可以解决吗?我尝试将输入json更改为:

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>
    }

但仍然无法正常工作。

3 个答案:

答案 0 :(得分:1)

尝试将您的方法签名更改为

private <T> T getResponse(final RestURI query, final TypeReference typeReference) 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, typeReference);
    }

    return response; //returns Map<String, AvailableInfo>
}

并以以下方式调用

TypeReference typeReference  = new TypeReference<Map<String, AvailableInfo>>() {
    };
Map<String, AvailableInfo> map = (Map<String, AvailableInfo>) getResponse(typeReference);

使用以下json示例

 {
        A:  { isAvailable : true} ,
        VV:  { isAvailable : false} ,
        B45:  { isAvailable : null} 
}

我尝试过并为我工作

答案 1 :(得分:0)

首先使用下面的行将其转换,然后可以处理数据:

List<? extends Map<String, String>> resultList = new ArrayList<LinkedHashMap<String, String>>();

答案 2 :(得分:0)

要将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}}