【问题标题】:How to Parse Array inside Object With Retrofit Android如何使用 Retrofit Android 解析对象内部的数组
【发布时间】:2026-01-04 08:45:01
【问题描述】:

我是Retrofit Library 的新手,我曾经使用 Volley 我正在尝试解析对象内部的数组,但我不知道该怎么做 这是我的 Json 回复

  {
        "response": {
            "code": "1",
            "success": true,
            "customers": [
                {
                    "id": 1,
                    "name": "reem",
                    "customer_type": "1",
                    "address": "45سسسس",
                    "mobile_no": "05684412211",
                    "phone_no": "414511555",
                    "created_at": "2018-07-30 08:26:48",
                    "updated_at": "2018-07-30 08:26:48"
                }
            ]
        }
    }

我想从响应响应中获取客户数组 这是客户模型:

public class Customer {

    @SerializedName("id")
    private Integer id;
    @SerializedName("customer_type")
    private Integer customer_type;
    @SerializedName("name")
    private String name;
    @SerializedName("address")
    private String address;
    @SerializedName("mobile_no")
    private String mobile_no;
    @SerializedName("phone_no")
    private String phone_no;

    public Customer(Integer id, Integer customer_type, String name, String address, String mobile_no, String phone_no) {
        this.id = id;
        this.customer_type = customer_type;
        this.name = name;
        this.address = address;
        this.mobile_no = mobile_no;
        this.phone_no = phone_no;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getCustomer_type() {
        return customer_type;
    }

    public void setCustomer_type(Integer customer_type) {
        this.customer_type = customer_type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getMobile_no() {
        return mobile_no;
    }

    public void setMobile_no(String mobile_no) {
        this.mobile_no = mobile_no;
    }

    public String getPhone_no() {
        return phone_no;
    }

    public void setPhone_no(String phone_no) {
        this.phone_no = phone_no;
    }
}

这里是数据服务接口:

@GET("get_customers")
    Call<List<Customer>> getAllCustomer();

你能帮我理解如何解析它吗?谢谢。

【问题讨论】:

    标签: java android arrays json retrofit


    【解决方案1】:

    创建另一个 POJO 类,它将具有这样的列表

    public class Response{
    
        @SerializedName("response")
        private Response response;
    
        @SerializedName("code")
        private String code;
    
        @SerializedName("success")
        private boolean success;
    
        @SerializedName("customers")
        private List<Customers> customers;
    
        public void setResponse(Response response){
            this.response = response;
        }
    
        public Response getResponse(){
            return response;
        }
    
        public void setCode(String code){
            this.code = code;
        }
    
        public String getCode(){
            return code;
        }
    
        public void setSuccess(boolean success){
            this.success = success;
        }
    
        public boolean isSuccess(){
            return success;
        }
    
        public void setCustomers(List<Customers> customers){
            this.customers = customers;
        }
    
        public List<Customers> getCustomers(){
            return customers;
        }
    }
    

    然后在您的数据服务接口中

    @GET("get_customers")
    Call<Response> getAllCustomer();
    

    然后你可以在从改造电话中得到身体后得到这样的客户列表

    reponse.getCustomers();
    

    【讨论】:

    • 如何制作POJO?
    • Android Studio 有这个插件 github.com/robohorse/RoboPOJOGenerator 在 Android Studio 中搜索 RoboPOJOGenerator 并安装/重启,然后将你的 json 粘贴到那里,它会自动为你创建类,或者你可以使用 jsonschema2pojo.org 站点还有
    • jsonschema2pojo.org ..target: java..Source type:json...Annotation style:GSON...并检查那些 Use double numbers..Include getter and setter....允许附加属性 ...粘贴您的回复 ....然后单击预览...@ReemAziz
    • 是的,或者您可以下载 zip,然后将其解压缩到项目的包位置,而不是预览,或者更好的是下载 Android Studio 插件
    【解决方案2】:

    正确,api应该只返回客户json列表

    或者你应该更新你的回应

        public class Customer
    {
        public int id { get; set; }
        public string name { get; set; }
        public string customer_type { get; set; }
        public string address { get; set; }
        public string mobile_no { get; set; }
        public string phone_no { get; set; }
        public string created_at { get; set; }
        public string updated_at { get; set; }
    }
    
    public class Response
    {
        public string code { get; set; }
        public bool success { get; set; }
        public List<Customer> customers { get; set; }
    }
    
    public class RootObject
    {
        public Response response { get; set; }
    }
    
        @GET("get_customers")
    Call<RootObject> getAllCustomer();
    

    【讨论】:

      【解决方案3】:

      当我们解析对象时,我们需要从 Json 的顶部开始。

      所以首先你需要得到“响应”键,然后是内部对象:客户的数组。

      为此添加 1 个名为 CustomersData.java 的类,其中包括 Response.java 对象、Response.java 客户容器数组:

      CustomersData.java:

      public class CustomersData {
          @SerializedName("response")
          public Response response;
      }
      

      Response.java

      public class Response {
      
              @SerializedName("code")
              public String code;
      
              @SerializedName("success")
              public boolean customers;
      
              @SerializedName("customers")
              public ArrayList<Customer> customers;
          }
      

      要使用 Retrofit 获取数据,可以这样调用:

      @GET("get_customers")
          Call< CustomersData > getAllCustomer();
      

      访问它使用:

      ArrayList <Customer> array = customersData.response.customers;
      

      【讨论】:

        【解决方案4】:

        使用这个website 来创建你的模型/pojo 类。稍后重命名它们。
        不要忘记在你的 pojo 类中添加@SeralizedName("name_like_your_json") 下面的代码。 (对于您要访问的每个变量)

        @SerializedName("id")
        private Integer id 
        

        您的模型类将如下所示。

        public class Customer {
        
        private Integer id;
        private String name;
        private String customerType;
        private String address;
        private String mobileNo;
        private String phoneNo;
        private String createdAt;
        private String updatedAt;
        
        public Integer getId() {
            return id;
        }
        
        public void setId(Integer id) {
            this.id = id;
        }
        
        public String getName() {
            return name;
        }
        
        public void setName(String name) {
            this.name = name;
        }
        
        public String getCustomerType() {
            return customerType;
        }
        
        public void setCustomerType(String customerType) {
            this.customerType = customerType;
        }
        
        public String getAddress() {
            return address;
        }
        
        public void setAddress(String address) {
            this.address = address;
        }
        
        public String getMobileNo() {
            return mobileNo;
        }
        
        public void setMobileNo(String mobileNo) {
            this.mobileNo = mobileNo;
        }
        
        public String getPhoneNo() {
            return phoneNo;
        }
        
        public void setPhoneNo(String phoneNo) {
            this.phoneNo = phoneNo;
        }
        
        public String getCreatedAt() {
            return createdAt;
        }
        
        public void setCreatedAt(String createdAt) {
            this.createdAt = createdAt;
        }
        
        public String getUpdatedAt() {
            return updatedAt;
        }
        
        public void setUpdatedAt(String updatedAt) {
            this.updatedAt = updatedAt;
        }}
        
        public class Example {
        
        private Response response;
        
        public Response getResponse() {
            return response;
        }
        
        public void setResponse(Response response) {
            this.response = response;
        }}
        
        
        public class Response {
        
        private String code;
        private Boolean success;
        private List<Customer> customers = null;
        
        public String getCode() {
            return code;
        }
        
        public void setCode(String code) {
            this.code = code;
        }
        
        public Boolean getSuccess() {
            return success;
        }
        
        public void setSuccess(Boolean success) {
            this.success = success;
        }
        
        public List<Customer> getCustomers() {
            return customers;
        }
        
        public void setCustomers(List<Customer> customers) {
            this.customers = customers;
        }}
        

        在你的界面中使用这个。

        @GET("get_customers")
        Call<Example> getAllCustomer();
        

        看上面的代码。您的 json 响应以 Json Object 开头。所以我们的调用必须是对象。在这种情况下,我们的对象是 Example 类。在此内部另一个对象称为 Response。在那个班级我们的设计

        【讨论】: