问题描述
我有一个没有任何对象(键)的JSON array
,其中有这样的JSON Objects
:
[
{
"Type": "Meeting",
"Name": "TestMeeting",
"StartDate": "2016-03-22T08:00:00",
"EndDate": "2016-03-24T09:00:00"
}
]
我试图解析它但找不到成功,谁能建议我如何使用Retrofit
解析这种类型的响应?
1楼
您可以定义一个表示 JSON 对象的Class
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Meeting{
@SerializedName("Type")
@Expose
private String type;
@SerializedName("Name")
@Expose
private String name;
@SerializedName("StartDate")
@Expose
private String startDate;
@SerializedName("EndDate")
@Expose
private String endDate;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
}
之后,您定义回调以进行改造,例如Call<List<Meeting>> getMeetings();
2楼
如果您正在使用 Gson 解析,那么简单地这样做
var model = Gson().fromJson(response.body()!!.string(), Array<Meeting>::class.java).toList()
3楼
杰森响应:
{"contacts":{"918888302649":0,"917207251056":0,"918888804581":0}}
POJO 类:
public class ContactSyncResponseModel {
@SerializedName("contacts")
@Expose
private Map<String, String> result;
public Map<String, String> getResult() {
return result;
}
public void setResult(Map<String, String> result) {
this.result = result;
} }