Alejandro H. Cruz Alejandro H. Cruz - 2 months ago 9
Android Question

Proper way to map complex JSON as a Java object with Retrofit and GSON

If the JSON Object I'm getting from the REST API has the following structure:

{

"IsError":false,

"Result":{ "key1":"value", "key2":"value", ... }

}


Should two different Java classes (IsError and Result) be composed or only one including both, as the expected Object in the Call<>?

Update 06th October 2016:

Proper usage with Retrofit2 asked in a new question

Answer

If all your services are going to respond with that structure, then I would implement a generic class that includes both IsError and Result:

public class Response<T> {
    private boolean IsError;
    private T Result;

    // getters
}

Then for each service you would have to use the corresponding classes, e.g. Response<MyClass1>, Response<MyClass2>, etc. as the expected object in the Call<>.