java - ArrayList<String> 使用改造 android 解析为 recyclerview

标签 java android json android-recyclerview retrofit

你好,我有一个 JSON但它是一个 ArrayList<String> 的数组我对这种类型的数组感到困惑我如何将这些类型的数组解析到我的 recyclerview 中?使用 Retrofit .

{
  "statusCode": 200,
  "success": true,
  "message": "Data get succesfully.",
  "data": [
    {
      "attributes_id": 20,
      "category_id": "3",
      "subcategory_id": "13",
      "product_id": "28",
      "attribute_name": "Make",
      "isRequired": "yes",
      "attribute_type": "dropdown",
      "attribute_options": [
        "LG",
        "Samsung",
        "Kelvinetor",
        "Haier",
        "Wrilphool",
        "Other"
      ],
      "created_at": "2019-07-01 12:02:18",
      "updated_at": "2019-07-01 12:02:57"
    }
  ]
}

这是我的 json我尝试将 attribute_options 列表放入 Recyclerview . 帮帮我。

这是我的回复:

public class GetAttributesResponse {

@SerializedName("statusCode")
@Expose
private int statusCode;

@SerializedName("success")
@Expose
private boolean success;

@SerializedName("message")
@Expose
private String message;

@SerializedName("data")
@Expose
private ArrayList<DataClass> dataclass = null;

public int getStatusCode() {
    return statusCode;
}

public void setStatusCode(int statusCode) {
    this.statusCode = statusCode;
}

public boolean isSuccess() {
    return success;
}

public void setSuccess(boolean success) {
    this.success = success;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public ArrayList<DataClass> getDataclass() {
    return dataclass;
}

public void setDataclass(ArrayList<DataClass> dataclass) {
    this.dataclass = dataclass;
}

public class DataClass {

    @SerializedName("attributes_id")
    @Expose
    private int attributes_id;

    @SerializedName("category_id")
    @Expose
    private String category_id;

    @SerializedName("subcategory_id")
    @Expose
    private String subcategory_id;

    @SerializedName("product_id")
    @Expose
    private String product_id;

    @SerializedName("attribute_name")
    @Expose
    private String attribute_name;

    @SerializedName("isRequired")
    @Expose
    private String isRequired;

    @SerializedName("attribute_type")
    @Expose
    private String attribute_type;

    @SerializedName("created_at")
    @Expose
    private String created_at;

    @SerializedName("updated_at")
    @Expose
    private String updated_at;

    @SerializedName("attribute_options")
    @Expose
    private ArrayList<String> attribute_options = null;

    public int getAttributes_id() {
        return attributes_id;
    }

    public void setAttributes_id(int attributes_id) {
        this.attributes_id = attributes_id;
    }

    public String getCategory_id() {
        return category_id;
    }

    public void setCategory_id(String category_id) {
        this.category_id = category_id;
    }

    public String getSubcategory_id() {
        return subcategory_id;
    }

    public void setSubcategory_id(String subcategory_id) {
        this.subcategory_id = subcategory_id;
    }

    public String getProduct_id() {
        return product_id;
    }

    public void setProduct_id(String product_id) {
        this.product_id = product_id;
    }

    public String getAttribute_name() {
        return attribute_name;
    }

    public void setAttribute_name(String attribute_name) {
        this.attribute_name = attribute_name;
    }

    public String getIsRequired() {
        return isRequired;
    }

    public void setIsRequired(String isRequired) {
        this.isRequired = isRequired;
    }

    public String getAttribute_type() {
        return attribute_type;
    }

    public void setAttribute_type(String attribute_type) {
        this.attribute_type = attribute_type;
    }

    public String getCreated_at() {
        return created_at;
    }

    public void setCreated_at(String created_at) {
        this.created_at = created_at;
    }

    public String getUpdated_at() {
        return updated_at;
    }

    public void setUpdated_at(String updated_at) {
        this.updated_at = updated_at;
    }

    public ArrayList<String> getAttribute_options() {
        return attribute_options;
    }

    public void setAttribute_options(ArrayList<String> attribute_options) {
        this.attribute_options = attribute_options;
    }
}

最佳答案

首先,您需要使用 Class Generator 创建 JSON 到 POJO 类.

package com.test.example;

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

public class Datum {

@SerializedName("attributes_id")
@Expose
private Integer attributesId;
@SerializedName("category_id")
@Expose
private String categoryId;
@SerializedName("subcategory_id")
@Expose
private String subcategoryId;
@SerializedName("product_id")
@Expose
private String productId;
@SerializedName("attribute_name")
@Expose
private String attributeName;
@SerializedName("isRequired")
@Expose
private String isRequired;
@SerializedName("attribute_type")
@Expose
private String attributeType;
@SerializedName("attribute_options")
@Expose
private List<String> attributeOptions = null;
@SerializedName("created_at")
@Expose
private String createdAt;
@SerializedName("updated_at")
@Expose
private String updatedAt;

public Integer getAttributesId() {
return attributesId;
}

public void setAttributesId(Integer attributesId) {
this.attributesId = attributesId;
}

public String getCategoryId() {
return categoryId;
}

public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}

public String getSubcategoryId() {
return subcategoryId;
}

public void setSubcategoryId(String subcategoryId) {
this.subcategoryId = subcategoryId;
}

public String getProductId() {
return productId;
}

public void setProductId(String productId) {
this.productId = productId;
}

public String getAttributeName() {
return attributeName;
}

public void setAttributeName(String attributeName) {
this.attributeName = attributeName;
}

public String getIsRequired() {
return isRequired;
}

public void setIsRequired(String isRequired) {
this.isRequired = isRequired;
}

public String getAttributeType() {
return attributeType;
}

public void setAttributeType(String attributeType) {
this.attributeType = attributeType;
}

public List<String> getAttributeOptions() {
return attributeOptions;
}

public void setAttributeOptions(List<String> attributeOptions) {
this.attributeOptions = attributeOptions;
}

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

}

package com.test.example;

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

public class FeasibilityModel {

@SerializedName("statusCode")
@Expose
private Integer statusCode;
@SerializedName("success")
@Expose
private Boolean success;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private List<Datum> data = null;

public Integer getStatusCode() {
return statusCode;
}

public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}

public Boolean getSuccess() {
return success;
}

public void setSuccess(Boolean success) {
this.success = success;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public List<Datum> getData() {
return data;
}

public void setData(List<Datum> data) {
this.data = data;
}

}

改造示例

 @GET("//Your End Point URL")
    Call<Datum> getList();

您的响应示例

call.enqueue(new Callback<List<Hero>>() {
            @Override
            public void onResponse(Call<Datum> call, Response<Datum> response) {

                //In this point we got our hero list
                //thats damn easy right ;) 

//now we can do whatever we want with this list

            }

            @Override
            public void onFailure(Call<Datum> call, Throwable t) {
                Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

引用链接:https://www.simplifiedcoding.net/retrofit-android-example/

关于java - ArrayList<String> 使用改造 android 解析为 recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56831126/

相关文章:

Java将int数组中的2位十进制转换为BCD

java - 如何重写 Java 中的类方法并向其添加 "throws"声明?

android - 将 Android 应用程序连接到 HTTP 服务器

android - Retrofit2:应为 BEGIN_ARRAY 但在第 1 行第 268 列路径 $[0].images 处为 STRING

java - 将 JSON 数据存储到 MySQL 数据库中

javascript - 在 JavaFX WebView 中加载 MathJax

java - 仅包含文本的 JLabel 如何在 JPanel 中对齐?

java - 从 ArrayList 中删除重复的对象

android - Android 中短而可变的间隔计时器

java - Json 和多个用户定义的对象错误