0

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'));
4
  • 2
    Can you edit and add the JSON? also is there a reason of not using wrapper class? Nov 6, 2018 at 10:09
  • Agreeing with Pranay. The error shows that your JSON's structure doesn't match your Apex data structure, but we don't know why.
    – David Reed
    Nov 6, 2018 at 11:16
  • Hi all, just realize that I need to typecast List<object> first then iterate over each object and extract the map.. Thanks btw for the response.. Nov 6, 2018 at 11:49
  • 3
    Can you post your answer as an answer? Then mark it as solving you problem. For tidiness - to make it easier for someone who googles that error message to see that there is a solution.
    – Aidan
    Nov 6, 2018 at 14:57

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.