Retrofit 2.0如何解析嵌套的JSON对象? [英] How can Retrofit 2.0 parse nested JSON object?

查看:122
本文介绍了Retrofit 2.0如何解析嵌套的JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的团队决定使用 Retrofit 2.0 ,我正在对该库进行一些初步研究.如标题中所述,我想通过Android应用中的Retrofit 2.0解析一些嵌套的JSON对象.

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": ...
        },
        {...},
        ...
        ]
}

我对items数组中的JSON对象感兴趣.我注意到有一些帖子关于如何通过解析嵌套的JSON对象Retrofit 1.X,但最新的Retrofit 2.0 API发生了很大变化,这使它们适应新API时会感到困惑.

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.

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

Two possible solutions come into my mind:

  1. 编写我自己的JSON转换器工厂,该工厂扩展了 Converter.Factory .
  2. 以String类型返回原始响应,然后由我自己解析.但是根据我的初步研究,要获得Retrofit 2.0的原始响应并不容易. Retrofit 2.0似乎坚持在将响应传递给我之前将其转换为某种东西,而Retrofit不提供自己的StringConverter. (我可能是错的〜)
  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~)

更新:实际上,我们可以通过将JSONElement设置为HTTP API接口的pojo并使用Retrofit提供的GSONConverter作为转换器来获得原始响应.

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看起来像

Assuming your complete JSON looks like

{
  "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
}

注意::这是用于JSON的类似解决方案.如果JSON具有其他键,则可以更改Items.java的成员.

Note : This is similar solution for your JSON. Members of Items.java can be changed if JSON has other keys.

将Pojo更新为新的JSON

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
}

这篇关于Retrofit 2.0如何解析嵌套的JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆