繁体   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