How does Retrofit 2.0 parse a nested JSON object?

Our team decided to use Retrofit 2.0, and I am doing some initial research on this library. As indicated in the header, I want to parse some nested JSON objects through Retrofit 2.0 in our Android app.

For example, here is a nested JSON object in 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 am interested in JSON objects inside the items array. I noticed that there are some reports about parsing nested JSON objects through Retrofit 1.X, but the latest Retrofit 2.0 APIs have changed a lot, which is confusing when adapting them to the new APIs.

Two possible solutions come to mind:

  1. Write my own factory of JSON converters that extends Converter.Factory .
  2. Return the raw response as a String type and parse it yourself. But itโ€™s not so easy to get a crude answer from Retrofit 2.0 according to my initial research. Retrofit 2.0 seems to insist on converting the response to something before passing it to me, and Retrofit does not provide its own StringConverter . (I could be wrong ~)

Update: in fact, we can get a raw response by setting JSONElement as a pojo for the HTTP API and using the GSONConverter provided by Retrofit as a converter.

+24
json android retrofit
05 Oct '15 at 6:43
source share
6 answers

Assuming your full 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 a similar solution for your JSON. The members of Item.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 } 
+28
Oct 05 '15 at 7:33
source share

The following code will help get a nested json object and an array

for example: json

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

First we need to create a model class, the element names of the model class are the same in the json element, which 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 an interface for modification to get the json element in the URL

 // don't need to put values of id in retrofit ex:: "/api-mobile_.php?method=getProductById&pid=" 

just pass the url parameter in the request, it automatically retrieves the url

eg:

 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()); } }); } 
+5
Aug 17 '16 at 7:33
source share

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

My assistants:

 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); } 
+4
Oct 05 '15 at 6:47
source share

Have you tried a volley? ... I prefer to convert it now, this is a google product. I have a working example, and if you don't mind, I can show you. http://www.androidhive.info/2014/09/android-json-parsing-using-volley/

0
05 Oct '15 at 6:54
source share

I forgot to add the @SerializedName and @Expose for the internal Class objects, and after adding the annotation problem is resolved. Like this:

JSON:

 {"Id": 1,} 

and class member:

 @SerializedName("Id") @Expose private int id; 
0
Oct. 25 '16 at 12:05
source share

Use this to create your class http://www.jsonschema2pojo.org

0
Jan 19 '19 at 13:11
source share



All Articles