在 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密钥。


我创建了一个通用PaginatedResponse<T>类来处理item_count,items_per_page和offset变量,如下所示:


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?


温温酱
浏览 139回答 1
1回答

冉冉说

由于您有不同的模型列表键<Model>s:,恕我直言,您最好为每个响应使用不同的模型。您必须private List<T> data;从基本响应模型中删除并将其移至子模型。我已经修改了您的代码并为您的和创建了一些示例products模型customers。下面给出详细的例子,BasePaginatedResponse.javapublic class BasePaginatedResponse {&nbsp; &nbsp; private int item_count;&nbsp; &nbsp; private int items_per_page;&nbsp; &nbsp; private int offset;&nbsp; &nbsp; public BasePaginatedResponse(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int item_count, int items_per_page, int offset) {&nbsp; &nbsp; &nbsp; &nbsp; this.item_count = item_count;&nbsp; &nbsp; &nbsp; &nbsp; this.items_per_page = items_per_page;&nbsp; &nbsp; &nbsp; &nbsp; this.offset = offset;&nbsp; &nbsp; }}客户响应.javapublic class CustomersResponse extends BasePaginatedResponse {&nbsp; &nbsp; private final List<Customer> customers;&nbsp; &nbsp; public CustomersResponse(int item_count, int items_per_page, int offset, List<Customer> customers) {&nbsp; &nbsp; &nbsp; &nbsp; super(item_count, items_per_page, offset);&nbsp; &nbsp; &nbsp; &nbsp; this.customers = customers;&nbsp; &nbsp; }&nbsp; &nbsp; public List<Customer> getCustomers() {&nbsp; &nbsp; &nbsp; &nbsp; return customers;&nbsp; &nbsp; }&nbsp; &nbsp; public class Customer {&nbsp; &nbsp; &nbsp; &nbsp; private final String id, name;&nbsp; &nbsp; &nbsp; &nbsp; public Customer(String id, String name) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public String getId() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}ProductsResponse.javapublic class ProductsResponse extends BasePaginatedResponse {&nbsp; &nbsp; private final List<Customer> products;&nbsp; &nbsp; public ProductsResponse(int item_count, int items_per_page, int offset, List<Customer> products) {&nbsp; &nbsp; &nbsp; &nbsp; super(item_count, items_per_page, offset);&nbsp; &nbsp; &nbsp; &nbsp; this.products = products;&nbsp; &nbsp; }&nbsp; &nbsp; public List<Customer> getProducts() {&nbsp; &nbsp; &nbsp; &nbsp; return products;&nbsp; &nbsp; }&nbsp; &nbsp; public class Customer {&nbsp; &nbsp; &nbsp; &nbsp; private final String id, name;&nbsp; &nbsp; &nbsp; &nbsp; public Customer(String id, String name) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public String getId() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在这里,我创建了 3 个类。1 个基本响应类(父类)和 2 个子类。父类包含两个子类共有的字段。当你使用Retrofit时,你ApiInterface应该是这样的interface ApiInterface{&nbsp; &nbsp; @GET("api/v1/customers")&nbsp; &nbsp; Call<CustomersResponse> getCustomers();&nbsp; &nbsp; @GET("api/v1/products")&nbsp; &nbsp; Call<ProductsResponse> getProducts();}如果您需要更多说明,请在评论中问我。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java