简体   繁体   中英

How to Parse Array inside Object With Retrofit Android

I am new of Retrofit Library , I used to use Volley I am trying to parse array inside the object but I can't get how to do it here is my Json response

  {
        "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"
                }
            ]
        }
    }

I want to take the customers array from the response response here is customer model :

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;
    }
}

and here is Data Service interface:

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

can you please help me to understand how to parse it and thank you .

Make another POJO class which will have List like this

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;
    }
}

Then in your Data Service Interface

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

And then you can get the List of customers like this after getting the body from retrofit call

reponse.getCustomers();

the right , api should return only list of customer json

or you should update your respone to

    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();

When we parse objects we need to start from top of Json.

So first you need to get "response" key, then it's inner objects : Customer's array.

For this add 1 class named CustomersData.java which includes Response.java object, Response.java container array of customers :

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;
    }

And to get data using Retrofit call it like :

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

Access it using :

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

Use this website to create your model/pojo class. Later rename them.
Don't forget to add @SeralizedName("name_like_your_json") this below code in your pojo class. (For every variable you want to access)

@SerializedName("id")
private Integer id 

Your model class will be look like this.

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;
}}

After this in your interface just use this.

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

Look at the above code. Your json response starts with Json Object. So Our call must be object. In this situation our object is Example class. Inside this another object called Response. In that class our desi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM