我想在JSON中使用Gson庫獲取JSON對象數組,但在這裏我遇到了一個問題,如何訪問嵌套JSON數組中的特定JSON數組。這是我正在嘗試:如何使用gson解析特定的對象數組?
我的模型類:
public class ComposeSMSList {
@SerializedName("userMessages")
@Expose
private List<UserMessage> userMessages = null;
@SerializedName("userPhoneBooks")
@Expose
private List<UserPhoneBook> userPhoneBooks = null;
@SerializedName("usersSendorIds")
@Expose
private List<UsersSendorId> usersSendorIds = null;
@SerializedName("userTemplates")
@Expose
private List<UserTemplate> userTemplates = null;
public List<UserMessage> getUserMessages() {
return userMessages;
}
public void setUserMessages(List<UserMessage> userMessages) {
this.userMessages = userMessages;
}
public List<UserPhoneBook> getUserPhoneBooks() {
return userPhoneBooks;
}
public void setUserPhoneBooks(List<UserPhoneBook> userPhoneBooks) {
this.userPhoneBooks = userPhoneBooks;
}
public List<UsersSendorId> getUsersSendorIds() {
return usersSendorIds;
}
public void setUsersSendorIds(List<UsersSendorId> usersSendorIds) {
this.usersSendorIds = usersSendorIds;
}
public List<UserTemplate> getUserTemplates() {
return userTemplates;
}
public void setUserTemplates(List<UserTemplate> userTemplates) {
this.userTemplates = userTemplates;
}
}
現在我想從父類訪問用戶的電話簿類。
有沒有優雅的方式來做到這一點?
我的代碼:
Gson mGson=new Gson();
List<ComposeModel> posts=Arrays.asList(mGson.fromJson(response.toString(),ComposeModel.class));
for (int i=0;i<posts.size();i++){
ComposeModel object = new ComposeModel(posts.get(i).getComposeSMSList().getUserMessages().get(i).getId(),posts.get(i).getComposeSMSList().getUserMessages().get(i).getMessage());
global.add(object);
}
mUserMsgAdpater=new UserMsgAdpater(mContext,global);
mRecyclerView.setAdapter(mUserMsgAdpater);
可以插入您的JSON的結構? –
「userMessages」:[ { 「id」:1, 「message」:「TODAY'S'S HONEY DROPS:\ n * //」Thanathu thavarai thaney unargiravan buthisaali!\\\\\ * \ n-BHAGAWAN SHREESATHYAM \ [email protected]」 },] 「userPhoneBooks」:[{ 「ID」:1, 「phonebookGroupname」: 「ISKON」, 「nrOfContacts」:2924} ,] – ashish
我四個陣列在我的JSON現在我想從整個JSON數組訪問用戶電話簿數組我怎樣才能實現這個 – ashish