I am using an geocoding API called Geocodio to process a batch of addresses. I felt I had a good handle on deserializing JSON arrays, but am having trouble deserializing a response that doesn't have properties explicitly defined. Instead it returns an object that maps results to my Salesforce Ids. Here is a sample response:
{
"results" : {
"001e000000JCXuFAAX" : {
"query" : "dfwdfw, New Ashford, MA 01237",
"response" : {
"error" : "Could not geocode address. Postal code or city required."
}
},
"001e000000JCv8hAAD" : {
"query" : "dfwdfw, Cambridge, MA 02138",
"response" : {
"error" : "Could not geocode address. Postal code or city required."
}
}
}
}
I choose to use JSON.deserializeUntyped()
to convert the string response to an object:
String s = response.getBody();
Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(s);
Object results = m.get('results');
However, I'm lost trying to figure out to access properties inside the object. Ideally, I'd like to do something like this:
for(Account act : actList) {
String id = (String) act.Id;
Object result = results.get(id);
//Now run logic to assign Latitude and Longitude values to Account object
}
Executing this code will throw: [OPERATION FAILED]: [Line: 29, Column: 21] Method does not exist or incorrect signature: [Object].get(String)
Object result = results.get(id);
Also tried, recasting it and worked. Thanks for the help! – Matthew Mitchener Nov 5 '14 at 21:11