這是我的Json。問題在於它有一個數組作爲節點。我並不是那麼流利地使用Retrofit,所以我在試圖理解正在發生的事情以及如何解決它時遇到了一些問題。使用Retrofit解析Json和JsonArray作爲根
[
{
"first_name":"Jakub",
"nickname":"",
"last_name":"Machalek",
"description":"description1",
"speaker_id":0,
"image_path":""
},
{
"first_name":"Krzysztof",
"nickname":"",
"last_name":"Opasik",
"description":"Description2",
"speaker_id":1,
"image_path":""
},
...etc...
我該如何解析它?我現在的嘗試是這樣的
的POJO
public class Speaker implements ParentListItem{
private String mFirstName;
private String mNickname;
private String mLastName;
private String mDescription;
private String mImageUrl;
private ArrayList<Speaker> mChildrenList;
public Speaker(String mFirstName, String mNickname, String mLastName, String mDescription, String mImageUrl) {
this.mFirstName = mFirstName;
this.mNickname = mNickname;
this.mLastName = mLastName;
this.mDescription = mDescription;
this.mImageUrl = mImageUrl;
mChildrenList = new ArrayList<>();
mChildrenList.add(this);
}
public String getFirstName() {
return mFirstName;
}
public void setFirstName(String mFirstName) {
this.mFirstName = mFirstName;
}
public String getNickname() {
return mNickname;
}
public void setNickname(String mNickname) {
this.mNickname = mNickname;
}
public String getLastName() {
return mLastName;
}
public void setLastName(String mLastName) {
this.mLastName = mLastName;
}
public String getDescription() {
return mDescription;
}
public void setDescription(String mDescription) {
this.mDescription = mDescription;
}
public String getImageUrl() {
return mImageUrl;
}
public void setImageUrl(String mImageUrl) {
this.mImageUrl = mImageUrl;
}
@Override
public List<?> getChildItemList() {
return mChildrenList;
}
@Override
public boolean isInitiallyExpanded() {
return false;
}
而且
public class SpeakerList {
List<Speaker> speakerList;
public List<Speaker> getSpeakerList() {
return speakerList;
}
public void add(Speaker nataCenterItem){
if(speakerList == null){
speakerList = new LinkedList<>();
}
speakerList.add(nataCenterItem);
}
改造 公共類SessionInfoService { 私人SessionInfoApi sessionInfoApi;
public SessionInfoService(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("...")
.addConverterFactory(GsonConverterFactory.create())
.build();
sessionInfoApi = retrofit.create(SessionInfoApi.class);
}
public void getLectureList(Callback callback) throws IOException {
sessionInfoApi.getLectureList().enqueue(callback);
}
public void getSpeakerList(Callback callback) throws IOException {
sessionInfoApi.getSpeakerList().enqueue(callback);
}
接口
@GET("...")
Call<List<Speaker>> getSpeakerList();
我可以使用任何幫助。
我已經接受了下面的答案。抱歉耽擱了。我是偶爾的用戶。現在我開始在工作中更頻繁地使用SO了。 – sp0rk