[SalesForce] Invalid conversion from runtime type ERROR

I'm trying to parse json response.. but keep getting this error

Invalid conversion from runtime type List ANY> to Map String,ANY>

Map<String, Object> MapLv3 = new Map<String, Object>();     
Integer x2 = 0;
for (Object oh : CallsList){
    MapLv3.put(String.valueof(x2), oh);
    x2++;
}
index = x2;
System.debug('Map Lv 3 ==>'+MapLv3);

Map<String, Map <String, Object>> Map3 = new Map<String, Map <String, Object>>();
for (Integer a2 = 0; a2 < index; a2++){
    Map3.put(String.valueof(a2),(Map<String,Object>) MapLv3.get(String.valueof(a2)));
}
System.debug('Segment ==>'+Map3.get('0').get('segments'));

Best Answer

just realize that I need to typecast List first then iterate over each object and extract the map

                //Level 3 -- SEGMENTS
                Map<String, Object> MapLv3 = (Map<String, Object>) arrayParticipants[0];     
                system.debug('Map Lv 3 ==>'+MapLv3);

                List<Object> arraySegments = new List<Object>();
                for (Object o : (List<Object>)MapLv3.get('segments')) {
                    arraySegments.add(o);
                }

                Map<String, Map<String,Object>> sgmnt = new Map<String, Map<String,Object>>();
                Integer i = 0;
                    for (Object finalSegment : arraySegments){
                        sgmnt.put(String.valueof(i), (Map<String, Object>) finalSegment);
                        i++;
                    }
Related Topic