23

In my Android project I have two types of response where both response are identical except two keys.

Response 1

{"fullName":"William Sherlock Scott Holmes","address":"221B Baker Street, London, England, UK","downloads":642,"rating":3,"repos":["https://link1","https://link2","https://link3"]}

Response 2

{"name":"Sherlock","city":"London","downloads":642,"rating":3,"repos":["https://link1","https://link2","https://link3"]}

If you see the responses only two key names are changing fullName/name and address/city

I don't want to create one more pojo for other response. My question is: is it possible to use only one Pojo to read both responses?

public class AccountInfo {
    private String name;
    private String city;
    //other objects

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
    //other setters and getters
}

Any help will be appreciated...

Mark Amery
  • 110,735
  • 57
  • 354
  • 402
Bharatesh
  • 8,375
  • 3
  • 34
  • 61

4 Answers4

43

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

Bharatesh
  • 8,375
  • 3
  • 34
  • 61
Doug Stevenson
  • 236,239
  • 27
  • 275
  • 302
1

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.

Farhad Faghihi
  • 9,874
  • 5
  • 27
  • 55
0

Yeah you can do that in a single POJO. Try this:

public class POJO {

@SerializedName("name")
public String name;

@SerializedName("city")
public String city;

@SerializedName("fullName")
public String fullName;

@SerializedName("address")
public String address;

@SerializedName("downloads")
public Integer downloads;

@SerializedName("rating")
public Integer rating;

@SerializedName("repos")
public List<String> repos = new ArrayList<String>();

}

While parsing you have to check values for null. For eg -

While Parsing Response 1: name and city variables will be null

While Parsing Response 2: fullname and address will be null

Note : Try checking values for null before using else you'll get nullpointerexception

Mayank Bhatnagar
  • 2,444
  • 1
  • 11
  • 19
0

Define all possible fields in your POJO Class like

    public class AccountInfo {
      private String name;
      private String city;
      private String fullname;
      private String address;
    }

While performing operation check for null in those feilds

BalaramNayak
  • 1,280
  • 13
  • 13