Skip to content Skip to sidebar Skip to footer

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 c

Solution 1:

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

publicclassResponse<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<>.

Post a Comment for "Proper Way To Map Complex Json As A Java Object With Retrofit And Gson"