You need to sign in to do that
Don't have an account?
Gabriel Mocelin
REST API: Invalid conversion from runtime type List<ANY> to Map<String,ANY>
Hi!
I'm trying to make an API call to populate some fields on a visual force page, but I'm catching this problem, does anyone have any idea what it could be?
System.TypeException: Invalid conversion from runtime type List<ANY> to Map<String,ANY> at line 31.
My debug at line 30:
I'm trying to make an API call to populate some fields on a visual force page, but I'm catching this problem, does anyone have any idea what it could be?
System.TypeException: Invalid conversion from runtime type List<ANY> to Map<String,ANY> at line 31.
public with sharing class pocAuthorizationManagement { public String email{get;set;} public String id_json{get;set;} public String inserted_at{get;set;} public String mobile_phone{get;set;} public String person_id{get;set;} public String term_id{get;set;} public String ip{get;set;} public String user_agent{get;set;} public pocAuthorizationManagement(ApexPages.StandardController stdController){ Lead lead = (Lead)stdController.getRecord(); lead = [SELECT Id, CPFTxt__c FROM Lead WHERE Id =: lead.Id]; String leadCPF = lead.CPFTxt__c; String requestEndPoint = 'my-end-point; Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint(requestEndPoint); request.setMethod('GET'); HttpResponse response = http.send(request); if(response.getStatusCode() == 200){ Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(response.getBody()); System.debug(results); Map<String, Object> dataResults = (Map<String, Object>)(results.get('data')); email = String.valueOf(dataResults.get('email')); id_json = String.valueOf(dataResults.get('id')); inserted_at = String.valueOf(dataResults.get('inserted_at')); person_id = String.valueOf(dataResults.get('person_id')); mobile_phone = String.valueOf(dataResults.get('mobile_phone')); term_id = String.valueOf(dataResults.get('term_id')); Map<String, Object> sessionResults = (Map<String, Object>)(dataResults.get('session_metadata')); ip = String.valueOf(dataResults.get('ip')); user_agent = String.valueOf(dataResults.get('user_agent')); } else { ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Algo de errado não está certo'); ApexPages.addMessage(msg); } } }Exemple of my JSON.
{ "data":[ { "email":null, "id":"e729f678-8793-4793-a50a-2ad85b221139", "inserted_at":"2019-05-10T18:16:46", "mobile_phone":null, "person_id":"5557e08f-b569-47b9-993c-29fb85830724", "session_metadata":{ "ip":"xxx", "user_agent":"{\"OS\": \"Android\", \"ip\": \"xxx\", \"Vendor\": \"Google Inc.\", \"AppName\": \"Netscape\", \"Browser\": \"Chrome\", \"OSVersion\": 9, \"Plataform\": \"Linux armv8l\", \"UserAgent\": \"Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G9650 Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/8.2 Chrome/63.0.3239.111 Mobile Safari/537.36\", \"AppVersion\": \"5.0 (Linux; Android 9; SAMSUNG SM-G9650 Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/8.2 Chrome/63.0.3239.111 Mobile Safari/537.36\", \"BrowserVersion\": 63.03239111}" }, "term_id":"a174ac7e-be49-4ab2-8539-83e85367b866" }, { "email":null, "id":"c7cec531-4057-4684-a6b9-6422e5138b9d", "inserted_at":"2019-05-10T18:16:46", "mobile_phone":null, "person_id":"5557e08f-b569-47b9-993c-29fb85830724", "session_metadata":{ "ip":"xxx", "user_agent":"{\"OS\": \"Android\", \"ip\": \"xxx\", \"Vendor\": \"Google Inc.\", \"AppName\": \"Netscape\", \"Browser\": \"Chrome\", \"OSVersion\": 9, \"Plataform\": \"Linux armv8l\", \"UserAgent\": \"Mozilla/5.0 (Linux; Android 9; SAMSUNG SM-G9650 Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/8.2 Chrome/63.0.3239.111 Mobile Safari/537.36\", \"AppVersion\": \"5.0 (Linux; Android 9; SAMSUNG SM-G9650 Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/8.2 Chrome/63.0.3239.111 Mobile Safari/537.36\", \"BrowserVersion\": 63.03239111}" }, "term_id":"a174ac7e-be49-4ab2-8539-83e85367b866" } ] }
My debug at line 30:
{data=({email=null, id=e729f678-8793-4793-a50a-2ad85b221139, inserted_at=2019-05-10T18:16:46, mobile_phone=null, person_id=5557e08f-b569-47b9-993c-29fb85830724, session_metadata={ip=xxx, user_agent={"OS": "Android", "ip": "xxx", "Vendor": "Google Inc.", "AppName": "Netscape", "Browser": "Chrome", "OSVersion": 9, "Plataform": "LinuxThanks for your help!
your problem is that your data is an Array of object and not yet and object itself.
thus you should say
then perform a loop like :
you can't skip the step of first parsing the list then doing the loop.
Phil
All Answers
Change: Map<String, Object> dataResults = (Map<String, Object>)(results.get('data'));
For: Map<String, Object> dataResults = new Map<String, Object>(results.get('data'));
if this solves your issue please mark this answer as correct. It may help others
When i change, i get this error, and cant save the file
Invalid initializer type Object found for Map<String,Object>: expected a Map with the same key and value types, or a valid SObject List
which is the structure of every object in that list?
your problem is that your data is an Array of object and not yet and object itself.
thus you should say
then perform a loop like :
you can't skip the step of first parsing the list then doing the loop.
Phil
Worked, thank you!
Is bringing the last node, what if I wanted to take the first one for example?