Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

android - How can Retrofit 2.0 parse nested JSON object?

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.

Two possible solutions come into my mind:

  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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

So Pojo classes would be

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 : This is similar solution for your JSON. Members of Items.java can be changed if JSON has other keys.


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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...