Your JSON is actually a JSON object containing a JSON object named response
. That JSON object has the format of your Pojo.
So one option is to create that hierarchy in Java
public class Wrapper {
private ErrorResponse response;
// getters & setters
}
And deserialize that
Wrapper wrapper = (Wrapper) gson.fromJson(new String(responseBody), Wrapper.class);
ErrorResponse errorResponse = wrapper.getResponse();
An alternative is to parse the JSON into a JsonElement
which you use to get the JSON object named response
and convert that one. Use the following types from the Gson library:
import com.google.gson.JsonParser;
import com.google.gson.GsonBuilder;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
//...
Gson gson = new GsonBuilder().create();
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(json);
ErrorResponse response = gson.fromJson(jsonElement.getAsJsonObject().get("response"), ErrorResponse.class);
Note that your class' field names must match the JSON or vice versa. result
vs Result
. Or you can use @SerializedName
to make it match
@SerializedName("Response")
private String response;