3

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.

1
  • why don't you parse it manually using optString()? Commented Jul 7, 2016 at 5:15

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

can you please change it according to my response as i am unable to change it , because i am having array within object and it is complex. Kindly do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.