如何在retrofit2中捕获两个不同对象数组的api响应? [重复]

问题描述 投票:1回答:3

这个问题在这里已有答案:

我如何在retrofit2中捕获以下响应?它有两种不同的对象类型。从api中检索后如何存储和使用它?这是我api响应的格式

{
"products": [
    {
        "categoryID": 2,
        "companyID": 1,
        "companyName": "Audi",
        "productID": 1001,
        "productName": "S6"
    },
    {
        "categoryID": 4,
        "companyID": 1,
        "companyName": "BWM",
        "productID": 1001,
        "productName": "Zen"
    },
    {
        "categoryID": 5,
        "companyID": 1,
        "companyName": "Ford",
        "productID": 1001,
        "productName": "Mustang"
    }],
"solutions": [
    {
        "companyID": 2,
        "companyName": "Audi",
        "solutionName": "A good plan",
        "solutionType": "Personalised",
        "working": "dsjhfgsdjhfgjsdh"
    },
    {
        "companyID": 2,
        "companyName": "djfhgkkl",
        "solutionName": "A good plan",
        "solutionType": "Personalised",
        "working": "asfh"
    }
]
}
java android api retrofit2
3个回答
1
投票

你可以创建一个POJO类,从这个对象中,你可以得到这里的数据是POJO

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

    @SerializedName("products")
    @Expose
    private List<Product> products = null;
    @SerializedName("solutions")
    @Expose
    private List<Solution> solutions = null;

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

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public List<Solution> getSolutions() {
        return solutions;
    }

    public void setSolutions(List<Solution> solutions) {
        this.solutions = solutions;
    }
}
-------------------------------com.example.Product.java-------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Product {
    @SerializedName("categoryID")
    @Expose
    private Integer categoryID;
    @SerializedName("companyID")
    @Expose
    private Integer companyID;
    @SerializedName("companyName")
    @Expose
    private String companyName;
    @SerializedName("productID")
    @Expose
    private Integer productID;
    @SerializedName("productName")
    @Expose
    private String productName;

    public Integer getCategoryID() {
        return categoryID;
    }

    public void setCategoryID(Integer categoryID) {
        this.categoryID = categoryID;
    }

    public Integer getCompanyID() {
        return companyID;
    }

    public void setCompanyID(Integer companyID) {
        this.companyID = companyID;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public Integer getProductID() {
        return productID;
    }

    public void setProductID(Integer productID) {
        this.productID = productID;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }
}
-------------------------------com.example.Solution.java-------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Solution {
    @SerializedName("companyID")
    @Expose
    private Integer companyID;
    @SerializedName("companyName")
    @Expose
    private String companyName;
    @SerializedName("solutionName")
    @Expose
    private String solutionName;
    @SerializedName("solutionType")
    @Expose
    private String solutionType;
    @SerializedName("working")
    @Expose
    private String working;

    public Integer getCompanyID() {
        return companyID;
    }

    public void setCompanyID(Integer companyID) {
        this.companyID = companyID;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getSolutionName() {
        return solutionName;
    }

    public void setSolutionName(String solutionName) {
        this.solutionName = solutionName;
    }

    public String getSolutionType() {
        return solutionType;
    }

    public void setSolutionType(String solutionType) {
        this.solutionType = solutionType;
    }

    public String getWorking() {
        return working;
    }

    public void setWorking(String working) {
        this.working = working;
    }
}

你可以使用http://www.jsonschema2pojo.org/从JSON生成你的POJO


0
投票

您可以在此处使用Gson解析并为产品和解决方案阵列添加@Nullable注释。在使用此数据时,检查arraylist的大小并检测列表中存在的数组。


0
投票

复制并确认您对Jsonschema2Pojo的回应,以获得您的回应的欲望Pojo。

© www.soinside.com 2019 - 2024. All rights reserved.