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

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


故事:我想读一次 Json 页面。没关系,我对此没有任何问题。我只能读取第一个单元格中的数据。不同类别的其他细胞。而且我通过使用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) {


        }

    });

      }


缥缈止盈
浏览 144回答 2
2回答

森林海

好吧,我做到了,这是我的解决方案。它有效。&nbsp;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() {&nbsp; &nbsp; return featured;}public void setFeatured(List<FeaturedItem> featured) {&nbsp; &nbsp; this.featured = featured;}public List<ProductsItem> getProducts() {&nbsp; &nbsp; return products;}public void setProducts(List<ProductsItem> products) {&nbsp; &nbsp; this.products = products;}public List<CategoriesItem> getCategories() {&nbsp; &nbsp; return categories;}public void setCategories(List<CategoriesItem> categories) {&nbsp; &nbsp; this.categories = categories;}public List<CollectionsItem> getCollections() {&nbsp; &nbsp; return collections;}public void setCollections(List<CollectionsItem> collections) {&nbsp; &nbsp; this.collections = collections;}public List<ShopsItem> getShops() {&nbsp; &nbsp; return shops;}public void setShops(List<ShopsItem> shops) {&nbsp; &nbsp; this.shops = shops;}public void setType(String type){&nbsp; &nbsp; this.type = type;}public String getType(){&nbsp; &nbsp; return type;}public void setTitle(String title){&nbsp; &nbsp; this.title = title;}public String getTitle(){&nbsp; &nbsp; return title;}@Overridepublic String toString(){&nbsp; &nbsp; if(type.equals("featured")){&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Featured Olanlar{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "featured = '" + featured + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ",type = '" + type + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ",title = '" + title + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "}";&nbsp; &nbsp; }&nbsp; &nbsp; else if(type.equals("new_products")){&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "En Yeni Ürünler{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "products = '" + products + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ",type = '" + type + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ",title = '" + title + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "}";&nbsp; &nbsp; }&nbsp; &nbsp; else if(type.equals("categories")){&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Kategoriler{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "categories = '" + categories + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ",type = '" + type + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ",title = '" + title + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "}";&nbsp; &nbsp; }&nbsp; &nbsp; else if(type.equals("collections")){&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Koleksiyonlar{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "collections = '" + collections + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ",type = '" + type + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ",title = '" + title + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "}";&nbsp; &nbsp; }&nbsp; &nbsp; else if(type.equals("editor_shops")){&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Editör Seçimi Vitrinler{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "shops = '" + shops + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ",type = '" + type + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ",title = '" + title + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "}";&nbsp; &nbsp; }&nbsp; &nbsp; else if(type.equals("new_shops")){&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "En Yeni Vitrinler{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "shops = '" + shops + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ",type = '" + type + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ",title = '" + title + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "}";&nbsp; &nbsp; }&nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }

慕神8447489

可以解析同一级别的对象的不同命名。在一种方式中,可以定义@SerializedName(value="name1", alternate={"name2", "name3"}) String b;含义,定义对象的替代名称。但要小心这个对象需要有相同的数据。在您的情况下,它将在所有情况下解析"id","name",因为 json 在所有情况下都包含这两个字段。另一方面,当您想要解析所有字段时,最好使用自定义JsonDeserializer。您可以在此处找到如何为 GSON 库编写自己的反序列化器的示例:反序列化器
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java