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
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;
}
}