5

I am trying to modify my code to resolve this error, but not sure why it isn't working. I am returning an array of JSON objects and the json is valid. What do I need to modify in the code below to resolve this error?

Error message: Error: Method does not exist or incorrect signature: void get(String) from the type Object

 HttpResponse response = h.send(request);

 if (response.getStatusCode() == 200) {

   List<Object> arr =(List<Object>)JSON.deserializeUntyped(response.getBody());
   Map<String, Object> dataNode = (Map<String, Object>)arr[0].get('Data');

   System.debug('data node ------------->' + dataNode);
}
1
  • (Map<String, Object>)((Map<String, Object>)arr[0]).get('Data');
    – kurunve
    Sep 2, 2019 at 22:32

1 Answer 1

8

In addition of casting the Object at the first index of arr to a map you also need to cast the Object that comes back from Data to a Map<string, Object> as well.

String jsonInput = '[{\n' +
    ' "Data" : ' + 
      '{ "height" : 5.5 , ' + 
        '"width" : 3.0 , ' + 
        '"depth" : 2.2 }\n' +
    '},\n' +
    '{\n' +
    ' "Data" : ' + 
      '{ "height" : 5.5 , ' + 
        '"width" : 3.0 , ' + 
        '"depth" : 2.2 }' +
    '}]';

List<Object> arr =(List<Object>)JSON.deserializeUntyped(jsonInput);
Map<String, Object> dataNode = (Map<String, Object>)((Map<String, Object>)arr[0]).get('Data');
System.debug(dataNode);

As that is starting to get unwieldy due to the multiple casts it would be helpful to split it out to a few lines.

List<Object> arr =(List<Object>)JSON.deserializeUntyped(jsonInput);
Map<String, Object> firstMap = (Map<String, Object>)arr[0];
Map<String, Object> dataNodeMap = (Map<String, Object>)firstMap.get('Data');
System.debug(dataNodeMap);

You must log in to answer this question.

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