0

I have the following where the result format of the callout is the following:

[
  {
    "uid": "72852a55-5be3-496c-904c-fc3fd261f0a4",
    "name": "Responseev Agency"
  },
  {
    "uid": "66852a55-5483-496c-904c-fc3fbf48f0a4",
    "name": "Haiti Agency"
  }
]

I am doing a callout as follows:

HttpResponse response = http.send(request);
    if (response.getStatusCode() == 200) {
        List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody());
        system.debug('results '+results);

        for (Object agency: results) {
            System.debug('(Map<String, Object>)agency'+(Map<String, Object>)agency); 
            System.debug('agency'+agency); 
        }
    }

system.debug('results '+results);
06:06:10:374 USER_DEBUG [16]|DEBUG|results ({name=AAA, uid=0000-0000-0000-0000})

System.debug('(Map<String, Object>)agency'+(Map<String, Object>)agency); 
(Map<String, Object>)agency{name=AAA, uid=0000-0000-0000-0000}

System.debug('agency'+agency); 
agency{name=AAA, uid=0000-0000-0000-0000}

Shouldnt I be able to get the uid as the folowing?

System.debug('agency'+agency.get('uid'));

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

1

Because agent is still object type so you need to typecast into map and then on the map, you can use get method. Currently you are trying to use on object variable which will not work.

for (Object agency: results) {
    System.debug('(Map<String, Object>)agency'+(Map<String, Object>)agency); 
    Map<String, Object> agencymap = (Map<String, Object>)agency;
    for(String str : agencymap.keyset()) {
        System.debug('agency  '+agencymap.get(str));
    }
}
| improve this answer | |
1

Setp 1 : Create the class JsonParser

Step 2 : Replace the line List results = (List) JSON.deserializeUntyped(response.getBody()); with

List<JsonParser> parseObjList = JsonParser.parse(response.getBody());

Step 3: Now you can use parseObjList the way u want it.

public class JsonParser {

    public String uid {get;set;} 
    public String name {get;set;} 

    public JsonParser(JSONParser parser) {
        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    if (text == 'uid') {
                        uid = parser.getText();
                    } else if (text == 'name') {
                        name = parser.getText();
                    } else {
                        System.debug(LoggingLevel.WARN, 'JsonParser consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }


    public static List<JsonParser> parse(String json) {
        System.JSONParser parser = System.JSON.createParser(json);
        return arrayOfJSON2Apex(parser);
    }

    public static void consumeObject(System.JSONParser parser) {
        Integer depth = 0;
        do {
            System.JSONToken curr = parser.getCurrentToken();
            if (curr == System.JSONToken.START_OBJECT || 
                curr == System.JSONToken.START_ARRAY) {
                depth++;
            } else if (curr == System.JSONToken.END_OBJECT ||
                curr == System.JSONToken.END_ARRAY) {
                depth--;
            }
        } while (depth > 0 && parser.nextToken() != null);
    }

    private static List<JsonParser> arrayOfJSON2Apex(System.JSONParser p) {
        List<JsonParser> res = new List<JsonParser>();
        if (p.getCurrentToken() == null) p.nextToken();
        while (p.nextToken() != System.JSONToken.END_ARRAY) {
            res.add(new JsonParser(p));
        }
        return res;
    }
}
| 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.