I am using Retrofit to get a JSON reply.
Here are parts of my implementation -
@GET("/api/report/list")
Observable<Bills> listBill(@Query("employee_id") String employeeID);
and the class Bills is -
public static class Bills {
@SerializedName("report")
public ArrayList<BillItem> billItems;
}
The BillItem
class is as follows -
public static class BillItem {
@SerializedName("id")
Integer entryID;
@SerializedName("employee_id")
Integer employeeDBID;
@SerializedName("type")
String type;
@SerializedName("subtype")
String subtype;
@SerializedName("date")
String date;
@SerializedName("to")
String to;
@SerializedName("from")
String from;
@SerializedName("amount")
Double amount;
@SerializedName("location")
String location;
@SerializedName("remark")
String remark;
@SerializedName("ispaid")
String isPaid;
@SerializedName("created_at")
String createdAt;
@SerializedName("updated_at")
String updateAt;
}
The problem is sometimes the REST API returns an Array of BillItem
objects, but sometimes it is just a key-value
pair. How does one handle such a situation?
When this response is received, everything works fine because the JSONArray
gets mapped to the ArrayList<BillItem>
-
{
"emp":{
"id":41,
"name":"",
"email":"",
"created_at":"2016-02-01 10:36:38",
"updated_at":"2016-02-01 10:36:38"
},
"report":[
{
"id":175,
"employee_id":41,
"type":"Travel",
"subtype":"Car",
"date":"2016-02-02 00:00:00",
"to":"gaha",
"from":"hshsj",
"amount":"64",
"location":"",
"remark":"shhs",
"ispaid":false,
"created_at":"2016-02-01 13:52:52",
"updated_at":"2016-02-01 13:52:52"
},
{
"id":179,
"employee_id":41,
"type":"Travel",
"subtype":"Car",
"date":"2016-02-01 00:00:00",
"to":"Gsh",
"from":"Dgdh",
"amount":"7646",
"location":"",
"remark":"Shsh",
"ispaid":false,
"created_at":"2016-02-01 14:39:48",
"updated_at":"2016-02-01 14:39:48"
}
]
}
But, sometimes the response is this, and it gives a JsonSyntaxException
-
{
"emp":{
"id":41,
"name":"",
"email":"",
"created_at":"2016-02-01 10:36:38",
"updated_at":"2016-02-01 10:36:38"
},
"report":{
"1":{
"id":175,
"employee_id":41,
"type":"Travel",
"subtype":"Car",
"date":"2016-02-02 00:00:00",
"to":"gaha",
"from":"hshsj",
"amount":"64",
"location":"",
"remark":"shhs",
"ispaid":false,
"created_at":"2016-02-01 13:52:52",
"updated_at":"2016-02-01 13:52:52"
},
"2":{
"id":179,
"employee_id":41,
"type":"Travel",
"subtype":"Car",":"2016-02-01 00:00:00",
"to":"Gsh",
"from":"Dgdh",
"amount":"7646",
"location":"",
"remark":"Shsh",
"ispaid":false,
"created_at":"2016-02-01 14:39:48",
"updated_at":"2016-02-01 14:39:48"
},
"0":{
"id":181,
"employee_id":41,
"type":"Travel",
"subtype":"Car",
"date":"2016-02-01 00:00:00",
"to":"ggg",
"from":"vg",
"amount":"0",
"location":"",
"remark":"cvv",
"ispaid":false,
"created_at":"2016-02-01 17:43:43",
"updated_at":"2016-02-01 17:43:43"
},
"3":{
"id":182,
"employee_id":41,
"type":"Travel",
"subtype":"Car",
"date":"2016-02-01 00:00:00",
"to":"Haha",
"from":"Ahah",
"amount":"0",
"location":"",
"remark":"Ahah",
"ispaid":false,
"created_at":"2016-02-01 17:53:58",
"updated_at":"2016-02-01 17:53:58"
}
}
}
How, does one deal with such a reply?