How to fetch JSON array without any key in Retrofit (Android)?

别等时光非礼了梦想. 提交于 2019-12-18 16:52:05

问题


I have a JSON array without any object(key) inside which there are JSON Objects like this :

[
  {
    "Type": "Meeting",
    "Name": "TestMeeting",
    "StartDate": "2016-03-22T08:00:00",
    "EndDate": "2016-03-24T09:00:00"
  }
]

I tried to parse it but can't find success,Can anyone suggest me how to parse this type of Response using Retrofit?


回答1:


You Can define a Class representing the JSON Object

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;
}

}

after that you define Callback for retrofit like that Call<List<Meeting>> getMeetings();




回答2:


if you are using Gson parsing then simple do it like this

 var model = Gson().fromJson(response.body()!!.string(), Array<Meeting>::class.java).toList()


来源:https://stackoverflow.com/questions/42204577/how-to-fetch-json-array-without-any-key-in-retrofit-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!