【发布时间】:2016-05-10 20:30:21
【问题描述】:
我正在使用 Retrofit 来获取 JSON 回复。
这是我的部分实现 -
@GET("/api/report/list")
Observable<Bills> listBill(@Query("employee_id") String employeeID);
Bills 类是 -
public static class Bills {
@SerializedName("report")
public ArrayList<BillItem> billItems;
}
BillItem类如下——
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;
}
问题是有时 REST API 返回一个 BillItem 对象数组,但有时它只是一个 key-value 对。如何处理这种情况?
收到此响应后,一切正常,因为 JSONArray 被映射到 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"
}
]
}
但是,有时响应是这样的,它给出了一个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"
}
}
}
如何处理这样的回复?
【问题讨论】:
-
在这个答案中查看 GSON 类型适配器 -- stackoverflow.com/a/33006947/1904517
-
你不能修复 api 以始终返回一个数组,即使只有 1 个项目,或者即使有 0 个项目?
-
@Gavriel 不幸的是,我对 API 没有任何控制权。
-
@iagreen 你能解释一下你的答案中的实现吗?
-
它使用
TypeAdapter总是返回一个列表,它偷看值。如果它是一个数组,它照常处理,如果不是,它读取值但在单个元素列表中返回它。
标签: java json retrofit rx-java