Skip to main content

System.JSONException: Expected AnimalLocator.Animal but found [line:1, column:2] 

 

 

public class AnimalLocator { 

 

    public class Animal { 

 

        public Integer id; 

 

        public String name; 

 

        public String eats; 

 

        public String says; 

 

    } 

 

    public class AnimalResult { 

 

        public Animal animal; 

 

    } 

 

public static String getAnimalNameById(Integer id) { 

 

        Http http = new Http(); 

 

        HttpRequest request = new HttpRequest(); 

 

        request.setEndpoint('

https://th-apex-http-callout.herokuapp.com/animals/

' + id); 

 

        request.setMethod('GET'); 

 

        HttpResponse response = http.send(request); 

 

        AnimalResult result = (AnimalResult) JSON.deserialize(response.getBody(), AnimalResult.class); 

 

        return (String)

result.animal.name

; 

 

   } 

 

} 

 

#Trailhead Challenges

답변 3개
  1. 10월 8일 오후 8:04

    Hi @Eduardo Oliveira Risse Xavier - Try this

    public class AnimalLocator {

    public static String getAnimalNameById(Integer indentifier) {

    String returnName ;

    Http http = new Http();

    HttpRequest request = new HttpRequest();

    request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + indentifier);

    request.setMethod('GET');

    HttpResponse response = http.send(request);

    // Parse the JSON response

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

    JSON2AnimalApex animal = JSON2AnimalApex.parse(response.getBody());

    returnName = animal.animal.name;

    }

    return returnName;

    }

    }