json 中多个不同的嵌套对象进行改造

Multiple different nested objects in json to retrofit

我的json图,请点击1 和 2。 希望足够理解。

故事: 我想读一遍 Json 页。没关系,我没问题 this.I 只能读取第一个单元格中的数据。不同 类 中的其他单元格。而且我还通过使用 http://www.jsonschema2pojo.org/ 获得了所有 类 重要!!每个不同的类型,包含不同的命名对象列表。

但是如何通过改造库将每个单元格中的数据设置为不同的类?

##精选列表##

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 + '\'' + 
        "}";
    }

主要活动

     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,输出

MainActivity: onResponse: FeaturedList{featured = '[FeaturedItem{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'}'…… --- 继续

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'}

可以解析同一级别对象的不同命名。一种方式是可以定义 @SerializedName(value="name1", alternate={"name2", "name3"}) String b; 意思是定义对象的备用名称。但要小心这个对象需要有相同的数据。在您的情况下,它将在所有情况下解析 "id""name",因为 json 包含这两个字段在所有情况下。

另一方面,当您想要解析所有字段时,最好使用自定义 JsonDeserializer。您可以在此处找到如何为 GSON 库编写自己的解串器的示例:Deserializer

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;
    }  
        }