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)

  • I answered but should have first asked exactly what is at line 29? – Keith C Nov 5 '14 at 21:09
  • You assumed correctly, it was Object result = results.get(id); Also tried, recasting it and worked. Thanks for the help! – Matthew Mitchener Nov 5 '14 at 21:11
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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.