Skip to main content
List<object> results = (List<object>) JSON.deserializeUntyped(response.getBody());

 

            for(Object o : results)

 

            { 

 

                Map<String, Object> mapobj = (Map<String, Object>)o;

 

Replaing last line of code with 

 

 Map<String, Object> mapobj = new Map<String, Object>(o)

 

geerates error: Invalid initializer type Object found for Map<String,Object>: expected a Map with the same key and value types, or a valid SObject List

 

Could you please explain how different these two last line of code works?
6 risposte
  1. 17 apr 2020, 13:59
    Ok,

     

    I suggest, please use the json2apex Tool which helps generate strongly typed Apex code for parsing a JSON structure.

     

    https://json2apex.herokuapp.com/

     

    Original JSON Response which you are assigning to List<object> results after parsing:

    [{"_id":"55d66226726b611100aaf741","replacement":false,"quantity":5,"name":"Generator 1000 kW","maintenanceperiod":365,"lifespan":120,"cost":5000,"sku":"100003"},{"_id":"55d66226726b611100aaf742","replacement":true,"quantity":183,"name":"Cooling Fan","maintenanceperiod":0,"lifespan":0,"cost":300,"sku":"100004"}]

0/9000