Search code examples
javaandroidjsonretrofitnested-object

Multiple different nested objects in json to retrofit


my json pic, please click 1 and 2. hopefully understandable enough.

Story: i want to read the Json page once. its okay, i have no problem with this.I can only read the data in the first cell. Other cells in different classes. And also i have all classes by using http://www.jsonschema2pojo.org/ important!!. Every different type, contains different named list of objects.

But how can i get the data in each cell to different classes by retrofit library?.

## featured list##

public class FeaturedList{
  //this my main class
@SerializedName("featured")
private List<FeaturedItem> featured;

@SerializedName("type")
private String type;

@SerializedName("title")
private String title;

public void setFeatured(List<FeaturedItem> featured){
    this.featured = featured;
}

public List<FeaturedItem> getFeatured(){
    return featured;
}

public void setType(String type){
    this.type = type;
}

public String getType(){
    return type;
}

public void setTitle(String title){
    this.title = title;
}

public String getTitle(){
    return title;
}

@Override
public String toString(){
    return 
        "FeaturedList{" + 
        "featured = '" + featured + '\'' + 
        ",type = '" + type + '\'' + 
        ",title = '" + title + '\'' + 
        "}";
    }

mainActivity

     restInsterface = ApiClient.getClient().create(RestInsterface.class);
    Call<List<FeaturedList>> listCall;
    listCall=restInsterface.getFeaturedList();
    listCall.enqueue(new Callback<List<FeaturedList>>() {
        @Override
        public void onResponse(Call<List<FeaturedList>> call, Response<List<FeaturedList>> response) {
            for (FeaturedList item:response.body()){
                Log.i(TAG, "onResponse: "+item.toString());
            }
        }

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

        }
    });
      }

Logcat, Output

MainActivity: onResponse: FeaturedList{featured = '[FeaturedItem{cover = 'Cover{thumbnail = 'Thumbnail{width = '270',url = 'https://cdn.vitrinova.com/images/jewelryafile-19eff39a-e6f9-4e56-9bf1-58ee0fd7920b.jpg',height = '135'}',width = '1080',medium = 'Medium{width = '540',url = 'https://cdn.vitrinova.com/images/jewelryafile-27bc984d-08ff-43ba-9b6e-0b267ddb7c56.jpg',height = '270'}'...… --- continue

MainActivity: onResponse: FeaturedList{featured = 'null',type = 'new_products',title = 'En Yeni Ürünler'}

MainActivity: onResponse: FeaturedList{featured = 'null',type = 'categories',title = 'Kategoriler'}

MainActivity: onResponse: FeaturedList{featured = 'null',type = 'collections',title = 'Koleksiyonlar'}

MainActivity: onResponse: FeaturedList{featured = 'null',type = 'editor_shops',title = 'Editör Seçimi Vitrinler'}

MainActivity: onResponse: FeaturedList{featured = 'null',type = 'new_shops',title = 'En Yeni Vitrinler'}


Solution

  • Okey I did, this is my solution. and it works.


     public class DataList{
    
    @SerializedName("featured")
    private List<FeaturedItem> featured;
    @SerializedName("products")
    private List<ProductsItem> products;
    @SerializedName("categories")
    private List<CategoriesItem> categories;
    @SerializedName("collections")
    private List<CollectionsItem> collections;
    @SerializedName("shops")
    private List<ShopsItem> shops;
    
    @SerializedName("type")
    private String type;
    
    @SerializedName("title")
    private String title;
    
    public List<FeaturedItem> getFeatured() {
        return featured;
    }
    
    public void setFeatured(List<FeaturedItem> featured) {
        this.featured = featured;
    }
    
    public List<ProductsItem> getProducts() {
        return products;
    }
    
    public void setProducts(List<ProductsItem> products) {
        this.products = products;
    }
    
    public List<CategoriesItem> getCategories() {
        return categories;
    }
    
    public void setCategories(List<CategoriesItem> categories) {
        this.categories = categories;
    }
    
    public List<CollectionsItem> getCollections() {
        return collections;
    }
    
    public void setCollections(List<CollectionsItem> collections) {
        this.collections = collections;
    }
    
    public List<ShopsItem> getShops() {
        return shops;
    }
    
    public void setShops(List<ShopsItem> shops) {
        this.shops = shops;
    }
    
    public void setType(String type){
        this.type = type;
    }
    
    public String getType(){
        return type;
    }
    
    public void setTitle(String title){
        this.title = title;
    }
    
    public String getTitle(){
        return title;
    }
    
    @Override
    public String toString(){
        if(type.equals("featured")){
            return
                    "Featured Olanlar{" +
                            "featured = '" + featured + '\'' +
                            ",type = '" + type + '\'' +
                            ",title = '" + title + '\'' +
                            "}";
        }
        else if(type.equals("new_products")){
            return
                    "En Yeni Ürünler{" +
                            "products = '" + products + '\'' +
                            ",type = '" + type + '\'' +
                            ",title = '" + title + '\'' +
                            "}";
        }
        else if(type.equals("categories")){
            return
                    "Kategoriler{" +
                            "categories = '" + categories + '\'' +
                            ",type = '" + type + '\'' +
                            ",title = '" + title + '\'' +
                            "}";
        }
        else if(type.equals("collections")){
            return
                    "Koleksiyonlar{" +
                            "collections = '" + collections + '\'' +
                            ",type = '" + type + '\'' +
                            ",title = '" + title + '\'' +
                            "}";
        }
        else if(type.equals("editor_shops")){
            return
                    "Editör Seçimi Vitrinler{" +
                            "shops = '" + shops + '\'' +
                            ",type = '" + type + '\'' +
                            ",title = '" + title + '\'' +
                            "}";
        }
        else if(type.equals("new_shops")){
            return
                    "En Yeni Vitrinler{" +
                            "shops = '" + shops + '\'' +
                            ",type = '" + type + '\'' +
                            ",title = '" + title + '\'' +
                            "}";
        }
        return null;
        }  
            }