3

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)

2
  • I answered but should have first asked exactly what is at line 29?
    – Keith C
    Nov 5, 2014 at 21:09
  • You assumed correctly, it was Object result = results.get(id); Also tried, recasting it and worked. Thanks for the help! Nov 5, 2014 at 21:11

1 Answer 1

6

You will need to cast the results to the type you know them to be so that the compiler knows that there is a get method:

Map<String, Object> results = (Map<String, Object>) m.get('results');

Repeat the casting as you go down through the layers.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .