首页 > 解决方案 > 如何从 REST API 响应创建 ArrayList 对象的 pojo 类?

问题描述

我正在开发必须在 API 上实现的项目。此 API 响应是 ArrayList 的对象。你能帮我创建它的 POJO 类,如果可能的话,它的实现。我正在使用改造 2 和 GSON。

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

就像,如果管理员添加Redmibrandsonly那么它将Redmi[]allorders

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

我的改造电话活动:

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

标签: javaandroidjsongsonretrofit2

解决方案


我不认为 POJO 模式在这种情况下会起作用,因为它总是会改变。如果与JSON Array of JSON Arrayallorders相同,那就更好了brandsonly

但是,如果无法更改,请查看以下内容。

    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();
            List<String> brands = new ArrayList<>();
            List<Map<String, Product>> products = new ArrayList<>();
            JSONObject jsonObject = new JSONObject(response1); 
            for(JSONObject brand : jsonObject.get("brandsonly")){
                 brands.add(brand.getvalue("brandname"));
            }

             if(brands.size= > 0){
                for(String brandname: brands){
                    HashMap<String, Product> tempHash = new HashMap<>();
                    JSONArray temp = jsonObject.getJSONArray(brandname);
                    foreach(JSONObject x : temp){
                           Product product = new Product();
                           product.FromJSONObject(x);
                           temp.put(brandname, product);
                    }
                    products.add(tempHash);
              }
           }
        }

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

因此,您有一个 BandNames 列表,其中包含每个产品的值。

我还建议查看jsonschema2.pojo


推荐阅读