Invalid conversion from runtime type LIST<ANY> to MAP<String,ANY>
If the response type is known and this is going to be an ongoing process, would creating a response class for that make sense?
public class MockResponse{
public String Id;
public String Name;
}
...
List<MockResponse> responseList = (List<MockResponse>)JSON.deserialize(res.getBody(), MockResponse.class);
According to the documentation, the method JSON.deserializeUntyped
has a return type of Object, which means it can return different types of information depending on what's being fed into it.
I suspect in your case res.getBody()
might be returning an array hence the inference of List
, though you'll need to post the body content to know for sure. If it is a list then I guess you'll want to have a list of maps, but without more information it's hard to say. You definitely can't cast a list to map, but maybe you could get what you're after simply by indexing the first list element:
Map<String, Object> data = (Map<String, Object>)(JSON.deserializeUntyped(res.getBody())[0]);
Are you sure that is where it is bombing. It would appear to be bombing on the line below:
List<Object> items = (List<Object>)data.get('Items');
You're attempting to cast a map (data) to a list and I don't believe that is allowed.