2

I'm trying to get the name value from an object. I tried everything but still dont get it. Any ideas?

    Http http = new http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/1');
    request.setMethod('GET');
    HttpResponse response = http.send(request);

    if(response.getStatusCode() == 200) {
        Map<String,Object> results = (Map<String,Object>)
            JSON.deserializeUntyped(response.getBody());
        Object animal = (Object) results.get('animal');
    }

The value of the response: {eats=chicken food, id=1, name=chicken, says=cluck cluck}

I'm trying to get the name value.

3

The {a=b, c=d, e=f} format tells you that this happens to be a Map. As such, you can cast your animal as:

Map<String, Object> animal = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());

From there, you can get the name value:

String animalName = (String)animal.get('name');

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.