Gson - Read a value with two different keys

Yes, you can totally use one POJO class for deserializing both responses. Your POJO class will contain keys from both responses.

public class Response {


private String name;
private String city;
private String fullName;
private String address;
private Integer downloads;
private Integer rating;
private List<String> repos ;

}

But when using the Response class, be careful that for first response, the name and city will be null, and for the second one, the address and fullname.


You can annotate the members to accept values from two different json names using the @SerializedName annotation:

@SerializedName(value = "name", alternate = {"fullName"})
private String name;
@SerializedName(value = "city", alternate = {"address"})
private String city;

Either named element can then be placed into the members that are annotated like this.

UPDATED : @SerializedName alternate names when deserializing is added in Version 2.4