Reputation: 4497
I am getting response from API, in that if there is data present then it is returning object and when data is not present it is returning blank array. I have model class for serializing this. Here is model class
@SerializedName("local")
public Local _local = new Local();
public class Local{
@SerializedName("total")
public String _total;
@SerializedName("providers")
public ArrayList<ProvidersDetails> _providers = new ArrayList<>();
public class ProvidersDetails{
@SerializedName("id")
public String _id;
@SerializedName("image")
public String _image;
}
@SerializedName("admin")
public transient Admin admin = new Admin();
public class Admin{
@SerializedName("id")
public String _id;
@SerializedName("first_name")
public String _first_name;
@SerializedName("last_name")
public String _last_name;
}
@SerializedName("orgn")
public Organization _organization = new Organization();
public class Organization{
@SerializedName("name")
public String _ name;
}
}
Here is some part of response i am getting from api
"local":{
"providers":[
{
"id":"1180",
"image":"photo.png"
},
{
"id":"1180",
"image":"photo.png"
},
{
"id":"1180",
"image":"photo.png"
}
],
"admin":{
"id":"1180",
"first_name":"nqgnerq",
"last_name":"gnejbeqp",
},
"orgn":{
"name":"organization name"
}
}
here is the other form which i am getting when data is not present
"local":{
"total":0,
"providers":[
],
"admin":[
],
"orgn":{
"name":"organization name"
}
}
I have checked many work arounds but failed, as i want to handle it in my pojo class. DO any one have solution, please suggest.
Upvotes: 3
Views: 1053
Reputation: 3994
You can handle it with "JsonDeserializer" .Here is an example.
Make class ProvidersDetails implements Serializable
Create a new class like this
public class ProvidersDetailsList {
public ArrayList<ProvidersDetails> getDetails() {
return providersDetails;
}
ArrayList<ProvidersDetails> providersDetails= new ArrayList<>();
}
Now write the Deserializer.
public class PhotoAlbumDeserializer implements JsonDeserializer {
@Override
public Object deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
ProvidersDetailsList providersDetailsList= new ProvidersDetailsList ();
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject != null) {
JsonElement usersField = jsonElement.getAsJsonObject().get("providers");
if (usersField == null || usersField.isJsonNull() || usersField.isJsonPrimitive())
; // if is null or is a primitive type will return an empty result
else if (usersField.isJsonObject()) {
providersDetailsList.getDetails().add((ProvidersDetails) jsonDeserializationContext.deserialize(usersField, ProvidersDetails.class));
} else if (usersField.isJsonArray()) {
Type listOfUserType = new TypeToken<List<ProvidersDetails>>() {
}.getType();
providersDetailsList.getDetails().addAll((Collection<? extends ProvidersDetails>) jsonDeserializationContext.deserialize(usersField, listOfUserType));
}
}
return providersDetailsList;
}
}
Now call it like this
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(ProvidersDetailsList.class, new PhotoAlbumDeserializer());
Gson gson = gsonBuilder.create();
ArrayList<ProvidersDetails> providersDetails = gson.fromJson(jsonString, ProvidersDetailsList.class);
Here is a link to do it with Gson https://stackoverflow.com/a/16591621/3111083
Upvotes: 3