[英]How to Parse Array inside Object With Retrofit Android
我是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();
能否請您幫我了解如何解析它,謝謝。
制作另一個POJO類,它將具有這樣的List
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();
在正確的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();
解析對象時,我們需要從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;
使用此網站可以創建模型/ 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。 在那堂課中,我們的設計
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.