[SalesForce] How to work with HttpResponse

I am able to do a successful callout and get the response just fine. Having issues referencing the data inside the response.getBody(). What method do you use to be able to reference the data sent back in the body?

Response body is saying:
{"success":true,"data":{"account_number":56789}}

How do I grab the Account Number to reference in the controller?

Best Answer

So, the response you are receiving is a JSON string. You can parse it like this:

String json = response.getBody();
Map<String,Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(json);

Then, you can access it like this:

Map<String,Object> dataMap = (Map<String,Object>)responseMap.get('data');
String accountNumber = (String)dataMap.get('account_number');