如何使用gson解析特定的对象数组?

时间:2017-10-17 13:56:12

标签: java android gson

我想使用Gson库在JSON中获取JSON对象数组,但是我在如何获取嵌套JSON数组中的特定JSON数组时遇到了问题。这就是我的尝试:

我的模特课程:

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

1 个答案:

答案 0 :(得分:0)

您的ComposeModel类是什么?无论如何,您在评论中插入的Json是不完整的。我认为是这样的:

<强> ComposeSMS.json

{
  "userMessages":[
    {
      "id":1,
      "message":"this is a message"
    }
  ],
  "userPhoneBooks":[
    {
      "id":1,
      "phonebookGroupname":"ISKON",
      "nrOfContacts":2924
    }
  ],
  "usersSendorIds":[
    {
      "id":1
    }
  ],
  "userTemplates":[
    {
      "id":1,
      "templateName":"aTemplate"
    }
  ]
}

如果你有 ComposeSMSList 类:

    public class ComposeSMSList {

    private List<UserMessage> userMessages = null;
    private List<UserPhoneBook> userPhoneBooks = null;
    private List<UsersSendorId> usersSendorIds = null;
    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;
    }
}

您可以通过以下方式反序列化您的json:

        Gson gson = new Gson();
        String filename="/pathTo.../composeSMS.json";
        JsonReader reader = new JsonReader(new FileReader(filename));
        ComposeSMSList composeSMSList= gson.fromJson(reader, ComposeSMSList.class);
        System.out.println("composeSMSList: " + gson.toJson(composeSMSList));

在您的问题中,您要求访问UserPhoneBook,但在您的代码中,您尝试使用UserMessage。

无论如何,你可以这样访问:

       List<UserPhoneBook> userPhoneBooks= composeSMSList.getUserPhoneBooks();
       for(UserPhoneBook upb: userPhoneBooks)
        {
            System.out.println("UserPhoneBook id: " + upb.getId());
            System.out.println("UserPhoneBook nrOfContacts: " +upb.getNrOfContacts());
            System.out.println("UserPhoneBook phonebookGroupname: " +upb.getPhonebookGroupname());
        }