如何在响应混合数据时进行POJO课。一些主要包含对象有的有数组类型的数据

问题描述:

我使用retrofit2炼成的网络请求,我已搜查过这里,但我的运气不好我找不到任何有效的解决方案。这就是为什么我在这里提出我的问题。 我的JSON的响应 e在下面给出。如何在响应混合数据时进行POJO课。一些主要包含对象有的有数组类型的数据

的问题是有时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": "[email protected]", 
        "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": "[email protected]", 
        "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": "[email protected]", 
        "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" 
        } 
       ] 
      ] 
     } 
    ] 
} 

我让我所有的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]; 
     } 
    }; 
} 
+1

使用http://www.jsonschema2pojo.org/以JSON转换成POJO –

+0

不工作.... – shahid17june

+0

[Rü越来越把类名和文件名小姐匹配错误 –

首先,如果可能的话,请验证您是否可以解决这个新空房禁地API。 :)

首先注意到API返回2种非常不同类型的数据:小时

  1. 列表(无钥匙)
  2. 地图的时间(每个元素有一个关键!)

你需要以某种方式代表你的POJO这种结构。也许列表中的键 - 小时对,如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/