簡體   English   中英

在GSON / Retrofit 2中使用動態鍵嗎?

[英]Using dynamic keys with GSON / Retrofit 2?

我有一個API,它對模型集合的所有請求都返回以下架構:

{
    item_count: 83,
    items_per_page: 25,
    offset: 25,
    <Model>s: [
        { ... },
        { ... },
        { ... },
        ...
    ]
}

例如,如果我向/api/v1/customers發出請求,則此JSON將包含一個customers密鑰。 如果我向/api/v1/products請求,則此JSON將包含一個products密鑰。

我將創建一個通用的item_count PaginatedResponse<T>類來處理item_countitems_per_pageoffset變量,如下所示:

public class PaginatedResponse<T> {
    private int item_count;
    private int items_per_page;
    private int offset;
    private List<T> data;

    public PaginatedResponse<T>(int item_count, int items_per_page, int offset, List<T> data) {
        this.item_count = item_count;
        this.items_per_page = items_per_page;
        this.offset = offset;
        this.data = data;
    }

    public List<T> getData() {
        return this.data;
    }
}

有沒有辦法將此JSON解析為我的PaginatedResponse POJO?

由於您對模型列表有不同的鍵, <Model>s: ,恕我直言,因此您最好為每個響應使用不同的模型。 您必須刪除private List<T> data; 從基本響應模型中移至子模型。

我已經修改了您的代碼,並為您的productscustomers創建了一些示例模型。 下面給出詳細示例,

BasePaginatedResponse.java

public class BasePaginatedResponse {

    private int item_count;
    private int items_per_page;
    private int offset;

    public BasePaginatedResponse(
            int item_count, int items_per_page, int offset) {
        this.item_count = item_count;
        this.items_per_page = items_per_page;
        this.offset = offset;
    }

}

CustomersResponse.java

public class CustomersResponse extends BasePaginatedResponse {

    private final List<Customer> customers;

    public CustomersResponse(int item_count, int items_per_page, int offset, List<Customer> customers) {
        super(item_count, items_per_page, offset);
        this.customers = customers;
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public class Customer {
        private final String id, name;

        public Customer(String id, String name) {
            this.id = id;
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public String getName() {
            return name;
        }
    }
}

ProductsResponse.java

public class ProductsResponse extends BasePaginatedResponse {

    private final List<Customer> products;

    public ProductsResponse(int item_count, int items_per_page, int offset, List<Customer> products) {
        super(item_count, items_per_page, offset);
        this.products = products;
    }

    public List<Customer> getProducts() {
        return products;
    }

    public class Customer {
        private final String id, name;

        public Customer(String id, String name) {
            this.id = id;
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public String getName() {
            return name;
        }
    }
}

在這里,我創建了3個類。 1個基本響應類(父類)和2個子類。 父類包含兩個子類共有的字段。

當您使用Retrofit ,您的ApiInterface應該是這樣的

interface ApiInterface{
    @GET("api/v1/customers")
    Call<CustomersResponse> getCustomers();

    @GET("api/v1/products")
    Call<ProductsResponse> getProducts();
}

如果您需要更多說明,請在評論中問我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM