Our team decide to use Retrofit 2.0 and I'm doing some initial research on this library. As stated in the title, I want parse some nested JSON objects via Retrofit 2.0 in our Android app.

For example, here is a nested JSON object with the format:

{
        "title": "Recent Uploads tagged android",
        "link": "https://www.flickr.com/photos/tags/android/",
        "description": "",
        "modified": "2015-10-05T05:30:01Z",
        "generator": "https://www.flickr.com/",
        "items": [
        {
            "title": ...
            "link": ...
            "media": {"m":"This is the value I want to get:)"}
            "description": ...
            "published": ...
            "author": ...
            "author_id": ...
            "tags": ...
        },
        {...},
        ...
        ]
}

I'm interested in the JSON objects inside items array. I notice there are some posts about parsing nested JSON objects via Retrofit 1.X, but the latest Retrofit 2.0 APIs has changed a lot, which is confusing when adapting them to the new APIs.

我想到了两种可能的解决方案:

  1. Write my own JSON converter factory which extends Converter.Factory.
  2. Return the raw response in a String type and parse it by myself. But it's not easy to get the raw response from Retrofit 2.0 according to my initial research. Retrofit 2.0 seems to insist in converting the response to something before pass it to me and Retrofit doesn't provide its own StringConverter. (I might be wrong~)

Update: We can actually get the raw response by setting JSONElement as the pojo for the HTTP API interface and use GSONConverter provided by Retrofit as the converter.

推荐答案

假设完整的JSON看起来像

{
  "title": "Recent Uploads tagged android",
  "link": "https://www.flickr.com/photos/tags/android/",
  "description": "",
  "modified": "2015-10-05T05:30:01Z",
  "generator": "https://www.flickr.com/",
  "items": [
    {
      "member1": "memeber value",
      "member2": "member value"
    },
    {
      "member1": "memeber value",
      "member2": "member value"
    }
  ]
}

所以Pojo课程将是

public class MainPojo {
    private String title; 
    private String description;
    private String link;
    private String generator;
    private String modified;
    private ArrayList<Items> items;

    // Getters setters
}

public class Items {
    private String member2;
    private String member1;

    // Getters setters
}

Note :对于JSON,这是类似的解决方案.项目的成员.如果JSON有其他键,可以更改java.


Update for Pojo as new JSON

public class Items {
    private String tags;
    private String author;
    private String title;
    private String description;
    private String link;
    private String author_id;
    private String published;
    private Media media;

    // Getters and Setters
}

public class Media {
    private String m;
    // Getters and Setters
}

Json相关问答推荐

当有嵌套数组而没有嵌套数组时,展平JSON

如何使用JQ有条件 Select 值

如何在PowerShell中扩展JSON中的嵌套数组

从json数组中删除特定元素

在解码的JSON哈希中搜索数组元素

Vegalite中分组条形图中的偏移量问题

将JSON数组组织到菜单中

Jolt:数组中两个字符串的连接

Python 将 struct 化文本转换和筛选为对象

如何使用 jq 在连续的 json 记录流上调用操作

Rails 控制器无需编码即可渲染 json

Servicestack 返回数组而不是带有数组的对象

N1QL 搜索对象内的某些对象

解析 JSON API 响应

Jolt - 在同一级别添加时组合值的问题

验证和格式化 JSON 文件

使用 GSON 解析嵌套的 JSON 数据

如何在不消除对象歧义的情况下使用 circe 解码 ADT

在android中读取Json数组

如何使用 SwiftyJSON 遍历 JSON?