1

I am trying to convert a Map to Map. I have a json and Object :

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

And the Object :

@Builder
@Getter
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class AvailableInfo {

    @JsonProperty("isAvailable")
    private Boolean isAvailable;

    public AvailableInfo() {
    }
}

I tried :

Map<String, AvailableInfo>  response = getResponse(query, Map.class);

But I am getting Error:

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

Below is the getResponse() method :

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

Any idea how it can be resolved? I tried changing the input json to :

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

But still not working.

7
  • The way I read that error message it's trying to cast the LinkedHashMap into the value. So you might actually have a Map<String, LinkedHashMap> ... (left as an exercise for the reader to determine the types of the LinkedHashMap parameter - e.g. is it Map<String, LinkedHashMap<String, String>> or something else? Commented Aug 22, 2018 at 4:51
  • @Yeah. the json is Map<String, LinkedHashMap> which i am trying to convert to Map<String, Object> Commented Aug 22, 2018 at 4:53
  • How are you calling getResponse? What is the value you pass to responseClass? Commented Aug 22, 2018 at 4:57
  • Possible duplicate of Jackson - Deserialize using generic class Commented Aug 22, 2018 at 4:59
  • This json: { A: { isAvailable : true} , VV: { isAvailable : false} , B45: { isAvailable : null} } should return you Map<String, AvailableInfo>. What's the error you are getting with this? Commented Aug 22, 2018 at 5:06

3 Answers 3

1

try to change your method signature to be

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

and call it in the following manner

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

using the following json example

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

i tried it and worked for me

Sign up to request clarification or add additional context in comments.

1 Comment

this is output of println in console {A=AvailableInfo@68be2bc2, VV=AvailableInfo@28feb3fa, B45=AvailableInfo@675d3402}
0

First convert it using below line and then you can play with the data:

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

1 Comment

I am trying to get a Map<String, AvailableInfo>
0

To convert the json into Map, the json has to be in the form of :

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

After that following code returns Map<String, AvailableInfo> :

Map<String, AvailableInfo> readValue = mapper.readValue(json, Map.class);

Output of System.out.println(readValue);:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.