【问题标题】:How to access sub-arraylist of arralist response from retrofit in android Java?如何从android Java中的改造访问arraylist响应的子arraylist?
【发布时间】:2020-11-02 06:41:31
【问题描述】:

我正在处理我必须在 API 上实现的项目。此 API 响应是 ArrayList 的对象。您能帮我了解如何访问子数组列表吗?我正在使用 Retrofit2 和 GSON。

如以下 JSON 架构所示,品牌名称将由管理员添加到 brandsonly 中,并将作为具有多个子对象的数组列表添加到 allorders 中。

例如,如果管理员在brandsonly 中添加Redmi,那么它将在allorders 中创建Redmi[]

{
    "status": "success",
    "brandsonly": [
        {
            "_id": "",
            "brandname": "SAMSUNG"
        },
        {
            "_id": "",
            "brandname": "VIVO"
        },
        {
            "_id": "",
            "brandname": "NOKIA"
        },
        {
            "_id": "",
            "brandname": "IPHONE"
        }
    ],
    "allorders": {
        "SAMSUNG": [],
        "VIVO": [],
        "NOKIA": [],
        "IPHONE": [
            {
                "_id": "",
                "order_id": "",
                "__v": 0,
                "adminconfirmation": 1,
                "finalpricetodeduct": 30950
            },
            {
                "_id": "",
                "order_id": "",
                "__v": 0,
                "adminconfirmation": 1,
                "finalpricetodeduct": 30950
            }
        ]
    }
}

我从活动中调用改造:

final AllOrdersResponse allOrdersResponse = new AllOrdersResponse(userID);
Call<AllOrdersResponse> responseCall = retrofit_interface.allOrderResponse(allOrdersResponse, "Bearer " + AuthToken);

    responseCall.enqueue(new Callback<AllOrdersResponse>() {
    @Override
    public void onResponse(@NotNull Call<AllOrdersResponse> call, @NotNull Response<AllOrdersResponse> response) {
        AllOrdersResponse response1 = response.body();

    }

    @Override
    public void onFailure(@NotNull Call<AllOrdersResponse> call, @NotNull Throwable t) {
        if (t instanceof SocketTimeoutException)
           Toast.makeText(context, "Socket Time out. Please try again.", Toast.LENGTH_LONG).show();
        else
            Toast.makeText(context, t.toString(), Toast.LENGTH_LONG).show();
    }
});

AllOrdersResponse POJO 类。

public class AllOrdersResponse {

    @SerializedName("status")
    public String status;
    @SerializedName("allorders")
    public Allorders allorders;
    public List<Brandsonly> brandsonly;

    public String getStatus() {
        return status;
    }

    public Allorders getAllorders() {
        return allorders;
    }

    public List<Brandsonly> getBrandsonly() {
        return brandsonly;
    }
}

还为每个品牌响应创建了BrandResponse POJO 类。

public class BrandResponse{

    @SerializedName("_id")
    public String ID;
    public String order_id;
    public String __v;
    public String adminconfirmation;
    public String finalpricetodeduct;

    //And getters also there
}

【问题讨论】:

  • AllOrdersResponse 类是什么样的?您只需为响应创建一个 POJO,然后访问其属性。你能用适当的细节和你面临的问题来编辑你的问题吗?
  • 我用AllOrdersResponse POJO 类更新了问题。请检查。
  • 在这种情况下 brandsonlyallorders 将具有相同的长度。由于allorders 具有动态键,我认为您可以在Map 中解析Allorders 并通过键(SAMSUNG、VIVO 等)获取值。检查This。地图是更好的选择,因为它可以有效地处理所有动态类型的品牌。
  • 好的,谢谢。这就是我一直在寻找的。​​span>

标签: java android json retrofit2 pojo


【解决方案1】:

如果你想解析未知的json,你应该使用this

Iterator<?> iterator = jsonObject.keys();
while (iterator.hasNext()) {
   String key = (String)iterator.next();
   //do what you want with the key.                 
}  

【讨论】:

  • 如果管理员在BrandsOnly 中添加品牌Redmi 怎么办?如何访问Redmi[]??
  • 你仍然可以用第二部分代码来解析它,只需要遍历'allorders' jsonObject的键。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 2016-10-09
  • 2012-02-22
相关资源
最近更新 更多