gpt4 book ai didi

java - 使用 Retrofit 时,当回复有时是对象有时是数组时,如何解析 JSON 回复?

转载 作者:行者123 更新时间:2023-12-03 06:07:23 26 4
gpt4 key购买 nike

我正在使用 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 反序列化器时,您可以检查 JsonElement 中的类型:

Gson gson = new GsonBuilder()
.registerTypeAdapter(BillItem.class, new BillItemDeserializer())
.registerTypeAdapter(Bills.class, new BillsDeserializer())
.create();

RestAdapter.Builder builder = new RestAdapter.Builder()
//...
.setConverter(new GsonConverter(gson));

public class BillsDeserializer implements JsonDeserializer<StringList> {

public Bills deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
BillItemList value = new BillItemList();
if (json.isJsonArray()) {
for (JsonElement element : json.getAsJsonArray()) {
value.add(gson.fromJson(element, BillItem.class));
}
} else {
value.add(gson.fromJson(element, BillItem.class));
}
return value;
}
}

参见示例:Gson deserialization - Trying to parse a JSON to an Object

关于java - 使用 Retrofit 时,当回复有时是对象有时是数组时,如何解析 JSON 回复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35146630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com