簡體   English   中英

響應包含混合數據時如何制作POJO類。 有些鍵包含Object,有些鍵具有數組類型數據

[英]How to Make POJO class when response have mix data. some key contain Object and some have array type data

我正在使用Retrofit2發出網絡請求,我在這里搜索過,但是不幸的是我找不到任何有效的解決方案。 這就是為什么我在這里提出我的問題。 我的JSON響應如下。

問題是有時REST API返回一個小時數組,但有時它只是一個對象 如何處理這種情況 在Retrofit / Gson中是否有一種優雅的方式來處理這樣的混合數組? 我對來自API的數據不承擔任何責任,因此我認為不可以更改它,任何幫助將不勝感激。

{
    "status": "success",
    "data": [


        {
            "id": 30,
            "name": "Rh.poutiqe",
            "global_delay": "0",
            "approved": true,
            "min_order": "0.000",
            "has_pickup": 0,
            "address": {
                "id": "35",
                "name": "Store Address",
                "type": "house",
                "block_number": "8",
                "street": "85",
                "avenue": "0",
                "building": "2",
                "floor": "",
                "apartment": "",
                "directions": "",
                "lat": null,
                "lng": null,
                "city": {
                    "id": "79",
                    "name": "Bayan",
                    "zone": "3",
                    "governate": "Hawally"
                }
            },
            "status": "Available",
            "owner": {
                "id": "32",
                "username": "+96550199900",
                "creation_date": "2017-08-07 09:46:49",
                "info": {
                    "name": "Asmaa alkandri",
                    "email": "Asooma_q8@hotmail.com",
                    "mobile": "50199900",
                    "store_id": "30",
                    "device_token": "e01efb2f03cd43509242c7b38ca890471db2e5b056f50b7a3661c34ab45b0b6e"
                },
                "addresses": [
                    {
                        "id": "35",
                        "name": "Store Address",
                        "type": "house",
                        "block_number": "8",
                        "street": "85",
                        "avenue": "0",
                        "building": "2",
                        "floor": "",
                        "apartment": "",
                        "directions": "",
                        "lat": null,
                        "lng": null,
                        "city": {
                            "id": "79",
                            "name": "Bayan",
                            "zone": "3",
                            "governate": "Hawally"
                        }
                    }
                ]
            },
            "open": false,
            "image": {
                "src": "https://api.bits.com.kw/assets/stores/30/y1nzl.jpg"
            },
            "hours": {
                "2": [
                    {
                        "id": "117",
                        "day_id": "2",
                        "day_of_week": "Tuesday",
                        "start_hour": "1400",
                        "end_hour": "2300"
                    }
                ],
                "3": [
                    {
                        "id": "118",
                        "day_id": "3",
                        "day_of_week": "Wednesday",
                        "start_hour": "1400",
                        "end_hour": "2300"
                    }
                ]
            },
            "next_available": {
                "day_of_week": "Tuesday",
                "start_hour": "1400",
                "end_hour": "2300",
                "day_id": "2",
                "date": "2017-09-19"
            }
        },

         {
            "id": 57,
            "name": "RH Kitchen",
            "global_delay": "0",
            "approved": true,
            "min_order": "0.000",
            "has_pickup": 0,
            "address": {
                "id": "63",
                "name": "Store Address",
                "type": "house",
                "block_number": "5",
                "street": "2",
                "avenue": "",
                "building": "97",
                "floor": "",
                "apartment": "",
                "directions": "",
                "lat": null,
                "lng": null,
                "city": {
                    "id": "79",
                    "name": "Bayan",
                    "zone": "3",
                    "governate": "Hawally"
                }
            },
            "status": "Not Receiving Orders",
            "owner": {
                "id": "57",
                "username": "+96566659454",
                "creation_date": "2017-09-09 11:32:19",
                "info": {
                    "name": "RH Kitchen",
                    "email": "taiba.aldarmi@gmail.com",
                    "mobile": "66659454",
                    "store_id": "57",
                    "device_token": "f6fffd3a393e9aea53863cffbb55b51a3afd2475e952091ea362a85fc930ec9a"
                },
                "addresses": [
                    {
                        "id": "63",
                        "name": "Store Address",
                        "type": "house",
                        "block_number": "5",
                        "street": "2",
                        "avenue": "",
                        "building": "97",
                        "floor": "",
                        "apartment": "",
                        "directions": "",
                        "lat": null,
                        "lng": null,
                        "city": {
                            "id": "79",
                            "name": "Bayan",
                            "zone": "3",
                            "governate": "Hawally"
                        }
                    }
                ]
            },
            "open": false,
            "image": {
                "src": "https://api.bits.com.kw/placeholder.jpg"
            },
            "hours": [],
            "next_available": false
        },
        {
            "id": 64,
            "name": "Lets__shop",
            "global_delay": "1440",
            "approved": true,
            "min_order": "5.000",
            "has_pickup": 0,
            "address": {
                "id": "64",
                "name": "Store Address",
                "type": "house",
                "block_number": "3",
                "street": "312",
                "avenue": "",
                "building": "56",
                "floor": "",
                "apartment": "",
                "directions": "",
                "lat": "0.000000000000000000",
                "lng": "0.000000000000000000",
                "city": {
                    "id": "126",
                    "name": "Saad Al Abdullah",
                    "zone": "9",
                    "governate": "Jahra"
                }
            },
            "status": "Available",
            "owner": {
                "id": "58",
                "username": "+96555899184",
                "creation_date": "2017-09-09 18:33:12",
                "info": {
                    "name": "Moneera ibrahim",
                    "email": "Moneeera96@gmail.com",
                    "mobile": "55899184",
                    "store_id": "64",
                    "device_token": null
                },
                "addresses": [
                    {
                        "id": "64",
                        "name": "Store Address",
                        "type": "house",
                        "block_number": "3",
                        "street": "312",
                        "avenue": "",
                        "building": "56",
                        "floor": "",
                        "apartment": "",
                        "directions": "",
                        "lat": "0.000000000000000000",
                        "lng": "0.000000000000000000",
                        "city": {
                            "id": "126",
                            "name": "Saad Al Abdullah",
                            "zone": "9",
                            "governate": "Jahra"
                        }
                    }
                ]
            },
            "open": true,
            "next_available": {
                "day_of_week": "Today",
                "start_hour": "1646",
                "end_hour": "2230",
                "day_id": "0",
                "date": "2017-09-17 1646"
            },
            "image": {
                "src": "https://api.bits.com.kw/assets/stores/64/0xqh4.jpg"
            },
            "hours": [
                [
                    {
                        "id": "161",
                        "day_id": "0",
                        "day_of_week": "Sunday",
                        "start_hour": "730",
                        "end_hour": "2230"
                    }
                ],
                [
                    {
                        "id": "162",
                        "day_id": "1",
                        "day_of_week": "Monday",
                        "start_hour": "730",
                        "end_hour": "2230"
                    }
                ],
                [
                    {
                        "id": "163",
                        "day_id": "2",
                        "day_of_week": "Tuesday",
                        "start_hour": "730",
                        "end_hour": "2230"
                    }
                ],
                [
                    {
                        "id": "164",
                        "day_id": "3",
                        "day_of_week": "Wednesday",
                        "start_hour": "730",
                        "end_hour": "2230"
                    }
                ],
                [
                    {
                        "id": "165",
                        "day_id": "4",
                        "day_of_week": "Thursday",
                        "start_hour": "730",
                        "end_hour": "2230"
                    }
                ],
                [
                    {
                        "id": "166",
                        "day_id": "5",
                        "day_of_week": "Friday",
                        "start_hour": "730",
                        "end_hour": "2230"
                    }
                ],
                [
                    {
                        "id": "167",
                        "day_id": "6",
                        "day_of_week": "Saturday",
                        "start_hour": "730",
                        "end_hour": "2230"
                    }
                ]
            ]
        }
    ]
}

我有像這樣的Make POJO類:

    public class StoreModel implements Parcelable{

    @SerializedName("id")
    public int id;

    @SerializedName("name")
    public String name;

    @SerializedName("global_delay")
    public String global_delay;

    @SerializedName("approved")
    public boolean approved;

    @SerializedName("min_order")
    public String min_order;

    @SerializedName("has_pickup")
    public int has_pickup;

    @SerializedName("address")
    public AddressModel address;

    @SerializedName("status")
    public String status;

    @SerializedName("owner")
    public OwnerModel owner;

    @SerializedName("open")
    public boolean open;

    @SerializedName("next_available")
    public Object next_available;

    @SerializedName("image")
    public ImageModel image;

    protected StoreModel(Parcel in) {
        id = in.readInt();
        name = in.readString();
        global_delay = in.readString();
        approved = in.readByte() != 0;
        min_order = in.readString();
        has_pickup = in.readInt();
        address = in.readParcelable(AddressModel.class.getClassLoader());
        status = in.readString();
        owner = in.readParcelable(OwnerModel.class.getClassLoader());
        open = in.readByte() != 0;
        image = in.readParcelable(ImageModel.class.getClassLoader());
        //next_available = in.readParcelable(NextAvailableModel.class.getClassLoader());
    }

    public static final Creator<StoreModel> CREATOR = new Creator<StoreModel>() {
        @Override
        public StoreModel createFromParcel(Parcel in) {
            return new StoreModel(in);
        }

        @Override
        public StoreModel[] newArray(int size) {
            return new StoreModel[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(id);
        parcel.writeString(name);
        parcel.writeString(global_delay);
        parcel.writeByte((byte) (approved ? 1 : 0));
        parcel.writeString(min_order);
        parcel.writeInt(has_pickup);
        parcel.writeParcelable(address, i);
        parcel.writeString(status);
        parcel.writeParcelable(owner, i);
        parcel.writeByte((byte) (open ? 1 : 0));
        parcel.writeParcelable(image, i);

        /*if(next_available instanceof NextAvailableModel)
            parcel.writeParcelable((NextAvailableModel)next_available, i);
        else if(next_available instanceof  Boolean)
            parcel.writeByte((byte) ((Boolean)next_available ? 1 : 0));*/
    }

    @SerializedName("hours")
    @Expose
    public List<List<HoursModel>> hours;


}


**And HoursModel Java Class**




    public class HoursModel implements Parcelable{

    @SerializedName("id")
    public String id;

    @SerializedName("day_id")
    public String day_id;

    @SerializedName("day_of_week")
    public String day_of_week;

    @SerializedName("start_hour")
    public String start_hour;

    @SerializedName("end_hour")
    public String end_hour;


    protected HoursModel(Parcel in) {
        id = in.readString();
        day_id = in.readString();
        day_of_week = in.readString();
        start_hour = in.readString();
        end_hour = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeString(day_id);
        dest.writeString(day_of_week);
        dest.writeString(start_hour);
        dest.writeString(end_hour);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<HoursModel> CREATOR = new Creator<HoursModel>() {
        @Override
        public HoursModel createFromParcel(Parcel in) {
            return new HoursModel(in);
        }

        @Override
        public HoursModel[] newArray(int size) {
            return new HoursModel[size];
        }
    };
}

首先,如果可能,請驗證是否可以修復此Braindead API。 :)

首先,請注意,API返回兩種截然不同的數據類型:

  1. 小時表(無鍵)
  2. 小時地圖(每個元素都有一個鍵!)

您需要以某種方式在POJO中表示此結構。 也許具有可空密鑰的密鑰-小時對的List<Pair<String, Hour> ,例如List<Pair<String, Hour> 您的來電。

其次,您需要創建一個自定義的TypeAdapter ,它可以反序列化列表和/或映射到Java對象。

一旦有了類型適配器,就可以使用@JsonAdapter批注將其附加到字段,或者在Gson構建器中注冊自定義類型。

https://google.github.io/gson/apidocs/com/google/gson/annotations/JsonAdapter.html

http://www.javacreed.com/gson-typeadapter-example/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM