1

1st

{
  "data": {
    "weather": [
      {
        "location": "location1",
        "condition": "condition1",
      },
      {
        "location": "location2",
        "condition": "condition2",
      }
    ],
    "total": 2
  },
  "sucess": true
}

2nd

{
  "data": {
    "syncRequired": true,
    "totalOfLocations": 0
  },
  "success": true
}

These 2 blocks are generally the response that I get from my API calls, and how I have been parsing it is by creating 2 different POJO classes for each response. Generally this is how it should look like for the 2nd block of json response.

public class JsonResponse {

    @SerializedName("success")
    @Expose
    private Boolean success;
   
    @SerializedName("data")
    @Expose
    private DataSync data;

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public DataSync getDataSync() {
        return data;
    }
    
    public void setDataSync() {
        this.data = data;
    }
}

and then parse inside the data key likewise

public class DataSync{

    @SerializedName("syncRequired")
    @Expose
    private Boolean syncRequired;

    @SerializedName("totalOfLocations")
    @Expose
    private Integer totalOfLocations;

    public Boolean isSyncRequired() {
        return syncRequired;
    }

    public void setSyncRequired(Boolean success) {
        this.syncRequired= syncRequired;
    }

    public Integer getTotalOfLocations() {
        return totalOfLocations;
    }

    public void setTotalOfLocations() {
        this.totalOfLocations= totalOfLocations;
    }
}

My aim is to use a single class of JsonResponse to parse every response that I get, and map the Data to their relevant Data classes for further parsing. Let me know if you need more information so that its easier to help me with my questions. Thanks in advance.

  • 1
    You can not use a Single class for parsing you have to create pojo classes for each different response . What you can do is define a base Contract for response and Use it for all the responses . Something [Like this](https://stackoverflow.com/a/63859992/4168607). – ADM Dec 30 '21 at 11:05
  • 1
    @ADM *You can not use a Single class for parsing* **this is not true** ... he can obviously create object with sum of all properties (of course they have to be optional/nullable) ... does it make sens - it depends , is it doable - **yes, it is** – Selvin Dec 30 '21 at 11:11
  • @Selvin you r right . there is no bound on number of fields in a class . So why not create one with 1000 fields . – ADM Dec 30 '21 at 11:12
  • I'm pretty sure that the question is wrong ... The question prolly is (which obviously is asked here again and again): How to parse json if response can be one of x ... of course answer can be found with internet search – Selvin Dec 30 '21 at 11:18
  • also ... he can make `data` `JSONObject` – Selvin Dec 30 '21 at 11:21
  • You guys have a good point. Thanks for the reference @ADM. – Harith Haziq Dec 31 '21 at 13:08
  • Thanks for the good insight @Selvin I have been looking up multiple references in the net and have understood that there are some cases where I could use a single POJO for multiple api parsing. But I am looking for the solution to a case where 'data' will return either Object or Array. At this point I guess the ones that does not need tedious work is to have a separate pojo classes for each of them. Will still be on the lookout though. – Harith Haziq Dec 31 '21 at 13:12

0 Answers0