31
votes

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.

5
What wil be in items array? Can you post that part too?Pankaj Kumar
@PankajKumar Updated:)hackjutsu
I have added an answer similar to your JSON. Try it.Pankaj Kumar
@PankajKumar Nice! As shown in my updated question, can I define private JSONObject media as a member in my pojo instead of defining a new Media class, since the value of the key media is also a JSON?hackjutsu
No. You need one more class for that.Pankaj Kumar

5 Answers

40
votes

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
}
11
votes

Following code will help to get nested json object and array

for example: json

{  
    "similar_product":[  
        {  .....
}
    ],
    "options":{  
        "Blouse Length":[  
            {  "value_id":"696556",
               }

first we need to create model class, model class items names are same in json item we can use @SerializedName("for exact json name")

public class Product {

       public Options options;

    public void setOptions(Options options) {
        this.options = options;
    }

    public Options getOptions() {
        return options;
    }


    // length...

    public class Options
    {
        @SerializedName("Blouse Length")
        private ArrayList<BlouseLength> blouseLengths;


        public void setBlouseLengths(ArrayList<BlouseLength> blouseLengths) {
            this.blouseLengths = blouseLengths;
        }

        public ArrayList<BlouseLength> getBlouseLengths() {
            return blouseLengths;
        }
    }



    public class BlouseLength {
        String value_id;
        public void setValue_id(String value_id) {
            this.value_id = value_id;
        }



        public String getValue_id() {
            return value_id;
        }
    }

}

create Interface for retrofit to get json item in url

// don't need to put values of id in retrofit 

ex:: "/api-mobile_.php?method=getProductById&pid="

just pass url parameter in query it automatically fetch the url

for example:

public interface Retrofit_Api {

    @FormUrlEncoded

    @GET("/api-mobile_.php?method=getProductById")
    Call<Product> responseproduct(@Query("pid") String pid);


}

In your Main class

     String pid=editid.getText().toString();


        final Retrofit adapter = new Retrofit.Builder()
                .baseUrl(Product_url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //Creating an object of our api interface
        Retrofit_Api api = adapter.create(Retrofit_Api.class);


        Call<Product> call = api.responseproduct(pid);


        call.enqueue(new Callback<Product>() {
            @Override
            public void onResponse(Call<Product> call, Response<Product> response) {


               ArrayList<Product.BlouseLength> p= new ArrayList(response.body().getOptions().getBlouseLengths());

Editadapter editadapter=new Editadapter(MainActivity.this,p);

                recyclerView.setAdapter(editadapter);


            }

            @Override
            public void onFailure(Call<Product> call, Throwable t) {


                Log.d("Error", t.getMessage());
            }
        });



    }
4
votes

Use Gson easy parsing for your models https://github.com/google/gson

My Helper Methods :

public String toJson(Object object) {
    return gson.toJson(object);
}

public <T> T fromJson(String json, Class<T> classOfT) {
    return gson.fromJson(json, classOfT);
}

public <T> T fromJson(JsonElement jsonElement, Class<T> classOfT) {
    return gson.fromJson(jsonElement, classOfT);
}
1
votes

have you tried volley? ...i prefer it over retrofit now that is a google product.I have working example and if u dont mind i can show you. http://www.androidhive.info/2014/09/android-json-parsing-using-volley/

0
votes

I forgot add @SerializedName and @Expose annotations for inner Class objects and after add these annotations problem solved. Like this:

JSON:

{"Id": 1,}

and Class member:

@SerializedName("Id")
@Expose
private int id;