0

I am using retrofit / okhttp. I had search through some question on handling nested JSON object with retrofit and i get my answer from here, which used several POJO classes according to the result.

Is that a simple way that i can direct parse the JSON object according to the element i required?
For example i had a JSON result

{
"DealsViewModel": {
"CountryDealsViewModel": {
  "CultureCountry": "string",
  "CultureLanguage": "string",
  "DealsPageInfo": [
    {
      "DisplayName": "string",
      "StartDate": "2018-05-07T06:43:31.179Z",
      "ExpiredDate": "2018-05-07T06:43:31.179Z",
      "Url": "string",
      "ImageUrl": "string",
      "ShortDescription": "string"
    }
  ],
  "CountryCode": "string",
  "CountryName": "string"
}
},
"Status": 0,
"Code": 0,
"Message": "string"
}


And from the result, if following the method from previous answer, i will need to have around 4 POJO file to handle the result but i will only need the "status" "code" "message" AND "DealsPageInfo"

Is that any way i can have something like this?

public class ExclusiveOffersParent {
private List<ExclusiveOffers> DealsPageInfo;
private String Message;
private int Status;
private int Code;

public ExclusiveOffersParent() {
    this.DealsPageInfo = new ArrayList<>();
}

public List<ExclusiveOffers> getDeals() {
    return DealsPageInfo;
}

public void setDeals(List<ExclusiveOffers> deals) {
    DealsPageInfo = deals;
}

public String getMessage() {
    return Message;
}

public void setMessage(String message) {
    Message = message;
}

public int getStatus() {
    return Status;
}

public void setStatus(int status) {
    Status = status;
}

public int getCode() {
    return Code;
}

public void setCode(int code) {
    Code = code;
}
}
ColdFire
  • 6,764
  • 6
  • 35
  • 51
Shawn.Y
  • 181
  • 3
  • 14

2 Answers2

0

You have to make pojo for all children:

  1. Goto http://www.jsonschema2pojo.org/

  2. Paste your json in the box

  3. Insert package and class name

  4. Select JSON from source type

  5. Select Gson from annotation style

  6. Press preview

This will gives you all the pojos required to deserialize your json response. You need to import gson also for that in your gradle file add following line in dependencies:

compile 'com.google.code.gson:gson:2.6.2'
Muhammad Saad Rafique
  • 3,158
  • 1
  • 13
  • 21
-1

this this my response of android studio Try this way :

 private void getResponseList() {
    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
    Call<Example> call = apiService.getResponse();
    call.enqueue(new Callback<Example>() {
        @Override
        public void onResponse(@NonNull Call<Example> call, @NonNull Response<Example> response) {
            assert response.body() != null;
            Log.e("Message",""+response.body().getMessage());
            Log.e("Code",""+response.body().getCode());
            Log.e("DisplayName",""+response.body().getDealsViewModel().getCountryDealsViewModel().getDealsPageInfo().get(0).getExpiredDate());
        }

        @Override
        public void onFailure(@NonNull Call<Example> call, @NonNull Throwable t) {
            // Log error here since request failed
            Log.e(TAG, t.toString());
        }
    });
}

===============================

 public class CountryDealsViewModel {

   @SerializedName("CultureCountry")
   @Expose
   private String cultureCountry;
   @SerializedName("CultureLanguage")
   @Expose
   private String cultureLanguage;
   @SerializedName("DealsPageInfo")
   @Expose
   private List<DealsPageInfo> dealsPageInfo = null;
   @SerializedName("CountryCode")
   @Expose
   private String countryCode;
   @SerializedName("CountryName")
   @Expose
   private String countryName;

   public String getCultureCountry() {
       return cultureCountry;
   }

   public void setCultureCountry(String cultureCountry) {
       this.cultureCountry = cultureCountry;
   }

   public String getCultureLanguage() {
       return cultureLanguage;
   }

   public void setCultureLanguage(String cultureLanguage) {
       this.cultureLanguage = cultureLanguage;
   }

   public List<DealsPageInfo> getDealsPageInfo() {
       return dealsPageInfo;
   }

   public void setDealsPageInfo(List<DealsPageInfo> dealsPageInfo) {
       this.dealsPageInfo = dealsPageInfo;
   }

   public String getCountryCode() {
       return countryCode;
   }

   public void setCountryCode(String countryCode) {
       this.countryCode = countryCode;
   }

   public String getCountryName() {
       return countryName;
   }

   public void setCountryName(String countryName) {
       this.countryName = countryName;
   }
}

==================================

public class DealsPageInfo {

   @SerializedName("DisplayName")
   @Expose
   private String displayName;
   @SerializedName("StartDate")
   @Expose
   private String startDate;
   @SerializedName("ExpiredDate")
   @Expose
   private String expiredDate;
   @SerializedName("Url")
   @Expose
   private String url;
   @SerializedName("ImageUrl")
   @Expose
   private String imageUrl;
   @SerializedName("ShortDescription")
   @Expose
   private String shortDescription;

   public String getDisplayName() {
       return displayName;
   }

   public void setDisplayName(String displayName) {
       this.displayName = displayName;
   }

   public String getStartDate() {
       return startDate;
   }

   public void setStartDate(String startDate) {
       this.startDate = startDate;
   }

   public String getExpiredDate() {
       return expiredDate;
   }

   public void setExpiredDate(String expiredDate) {
       this.expiredDate = expiredDate;
   }

   public String getUrl() {
       return url;
   }

   public void setUrl(String url) {
       this.url = url;
   }

   public String getImageUrl() {
       return imageUrl;
   }

   public void setImageUrl(String imageUrl) {
       this.imageUrl = imageUrl;
   }

   public String getShortDescription() {
       return shortDescription;
   }

   public void setShortDescription(String shortDescription) {
       this.shortDescription = shortDescription;
   }    
   }

===============================

public class DealsViewModel {

   @SerializedName("CountryDealsViewModel")
   @Expose
   private CountryDealsViewModel countryDealsViewModel;

   public CountryDealsViewModel getCountryDealsViewModel() {
       return countryDealsViewModel;
   }

   public void setCountryDealsViewModel(CountryDealsViewModel countryDealsViewModel) {
       this.countryDealsViewModel = countryDealsViewModel;
   }
  }

=============================

 public class Example {

   @SerializedName("DealsViewModel")
   @Expose
   private DealsViewModel dealsViewModel;
   @SerializedName("Status")
   @Expose
   private Integer status;
   @SerializedName("Code")
   @Expose
   private Integer code;
   @SerializedName("Message")
   @Expose
   private String message;

   public DealsViewModel getDealsViewModel() {
       return dealsViewModel;
   }

   public void setDealsViewModel(DealsViewModel dealsViewModel) {
       this.dealsViewModel = dealsViewModel;
   }

   public Integer getStatus() {
       return status;
   }

    public void setStatus(Integer status) {
       this.status = status;
   }

    public Integer getCode() {
       return code;
     }

   public void setCode(Integer code) {
       this.code = code;
     }

    public String getMessage() {
       return message;
     }

    public void setMessage(String message) {
        this.message = message;
    }  
 }
jeet parmar
  • 868
  • 8
  • 19