// Get the JSON String, usually from a Web Service, but for executabillity I picked our example JSON String jsonString = '{ "done" : true, "totalSize" : 10000, "records" : [ { "Id" : "0012z00000AVOkfAAH", "Name" : "Test Account - 00001"}, { "Id" : "0012z00000AVOmZAAX", "Name" : "Test Account - 10000" } ] }'; // Deserialize the JSON String and cast it to an Object Map Map<String,Object> objectMap = (Map<String,Object>) JSON.deserializeUntyped(jsonString); // We can now extract the Object List that we want to target based on the attribute name Object[] objectList = (Object[]) objectMap.get('records'); // We want to be able to read data from each object in the object list // we need to cast each object to it's own Object Map (Map<String,Object>>) // I chose a list to store all these maps so we can itterate it later List<Map<String,Object>> objectMapList = new List<Map<String,Object>>(); // Iterate the Object List for(Object obj : objectList){ // We cast the Object from the Object List to an Object Map and add it to the List of Object Maps Map<String,Object> objectMap = (Map<String,Object>) obj; objectMapList.add(objectMap); } // Call the processing logic handleRecordProcessing(objectMapList); /** * Some service method that does the magic that needs to be done to the records * Any processing will happen inside the loop */ void handleRecordProcessing(List<Map<String,Object>> objectMapsToProcess){ for(Map<String,Object> objectMap : objectMapsToProcess){ System.debug('Id: ' + objectMap.get('Id') + ' - Name:' + objectMap.get('Name')); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter