[SalesForce] System.TypeException: Invalid conversion from runtime type Map to Map

I have the following Program which gives me the following error:

System.TypeException: Invalid conversion from runtime type Map<String,ANY> to Map<String,String>

The program which I created is for reference:

String response = '{"access_token":"00D7F0000001I8v!ARgAQMUxsUNMleUjdSk8yn1YsyR1gJ4R8S24BQCfHTIBLpkFiJfEeXxKJARviUBRSAhCM84x96yTXVUXeZWbrlSoVnD729MG","instance_url":"https://ap5.salesforce.com","id":"https://login.salesforce.com/id/00D7F0000001I8vUAE/0057F000000l2bgQAA","token_type":"Bearer","issued_at":"1494093385165","signature":"VsM4LBk6pwK98r8JlxFDsRWKgOKscnQ4h5gwwqDN/Ns="}';
Map<String, String> resMap = (Map<String, String>) JSON.deserializeUntyped(response);

String accessToken = resMap.get('access_token');
System.debug('Access Token : '+accessToken);

Best Answer

Just deserialize it as a Map<String, Object>:

String response = '{"access_token":"...","other_properties":"..."}';
Map<String, Object> resMap = (Map<String, Object>)JSON.deserializeUntyped(response);

String accessToken = (String)resMap.get('access_token');
System.debug('Access Token : '+accessToken);
Related Topic