【问题标题】:Handling a complex JSON response with ArrayList inside another ArrayList在另一个 ArrayList 中使用 ArrayList 处理复杂的 JSON 响应
【发布时间】:2016-06-18 12:23:30
【问题描述】:

好的!我有一个非常复杂的 JSON,我使用 Retrofit 库通过异步进程对其进行了解析。

实际上,我正在使用 JSON API 解析 Wordpress JSON。我在 recyclerview 适配器中显示所有帖子的标题和内容。

不过,获取新帖子的标题和内容非常容易。我在为它创建 ModelClass 时遇到问题。以下是JSON格式:

    {
  "status": "ok",
  "count": 10,
  "count_total": 26,
  "pages": 3,
  "posts": [
    {
      "id": 274,
      "title": "A ver, very long post!",
      "content":"BLAH BLAH",
      "excerpt":"BLAH",
    "attachments": [
        {
          "id": 201,
          "url": "http://example.com/image.jpg",
          "slug": "iamge.jpg",
          "title": "iamge.jpg",
          "description": "",
          "caption": "",
          "parent": 274,
          "mime_type": "image/jpeg",
          "images": {
            "full": {
              "url": "http://example.com/image.jpg",
              "width": 480,
              "height": 480
            },
            "thumbnail": {
              "url": "http://example.com/image.jpg",
              "width": 150,
              "height": 150
            },
            "medium": {
              "url": "http://example.com/image.jpg",
              "width": 300,
              "height": 300
            },
            "medium_large": {
              "url": "http://example.com/image.jpg",
              "width": 480,
              "height": 480
            },
            "large": {
              "url": "http://example.com/image.jpg",
              "width": 480,
              "height": 480
            }
          }
        }
      ]
}

我的模型类包含以下类变量以及 getter 方法:

private int id;
    private String type;
    private String slug;
    private String url;
    private String status;
    private String title;
    private String title_plain;
    private String content;
    private String excerpt;
    private String date;
    private String modified;
    private String[] categories;
    private String[] tags;
    private String author;
    private String[] comments;
    public ArrayList<AttachmentClass> attachments = new ArrayList<AttachmentClass>();

我的问题在于 ArrayList 附件。附件是字符串类型数据和其他用户定义的类变量的集合......

我可以获取帖子、标题等。但我不知道如何从各种附件的 ArrayList 中获取数据...请帮助...

在适配器类中,我正在使用

private ArrayList<ModelClass> dataset;
String message = dataset.get(getLayoutPosition()).getContent();
String title = dataset.get(getLayoutPosition()).getTitle();

这些调用给了我结果......我只是不知道如何显示来自数据集的数组列表(附件)中的数据......

【问题讨论】:

    标签: java android json wordpress arraylist


    【解决方案1】:

    正如你所说,附件有一个对象数组。 您的 AttachmentClass 应包括所有字段:id、url、slug...以及包含字段的图像类别:完整、缩略图、中等...

    注意,在您的情况下,“图像”不是对象数组,它只是一个对象

    并使用 for() 循环从附件中获取所有项目 像这样: 为我) dataset.get(getLayoutPosition()).getAttachements(i).getImage().getFull()

    希望能帮到你:)

    【讨论】:

    • 但是,getAttachments() 是一个 ArrayList,我不能在上面使用 getImage()...
    • 也许像这样 for(int i =0; i
    • 哦!谢谢!这就像魔术一样!我一直在四处寻找解决办法……伙计,你就像一个天使!非常感谢...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2021-01-20
    • 1970-01-01
    • 2018-08-19
    • 2012-04-13
    • 1970-01-01
    • 2022-06-12
    相关资源
    最近更新 更多