2015-10-28 21 views
1

我要讓GET請求,並使用不同的參數名稱來獲取對象數組:改造獲得與不同的對象

{ 
    "monday": { 
    "open_time": "06:30", 
    "close_time": "20:30" 
    }, 
    "tuesday": { 
    "open_time": "06:30", 
    "close_time": "20:30" 
    }, 
    "wednesday": { 
    "open_time": "06:30", 
    "close_time": "20:30" 
    }, 
    "thursday": { 
    "open_time": "06:30", 
    "close_time": "20:30" 
    }, 
    "friday": { 
    "open_time": "06:30", 
    "close_time": "20:30" 
    }, 
    "saturday": { 
    "open_time": "08:00", 
    "close_time": "19:00" 
    }, 
    "sunday": { 
    "open_time": "08:00", 
    "close_time": "19:00" 
    } 
} 

所以我做了DayEntity,但不知道如何使用我的週一,週二ECT? 服務jsonschema2pojo希望從週一到週日創建大量的課程。

public class DayEntity { 

    @SerializedName("open_time") 
    @Expose 
    private String openTime; 

    @SerializedName("close_time") 
    @Expose 
    private String closeTime; 

    public void setOpenTime(String openTime) { 
     this.openTime = openTime; 
    } 

    public void setCloseTime(String closeTime) { 
     this.closeTime = closeTime; 
    } 

UDP:如果GSON可以解析很好如何與改造相結合使用嗎?我有WeekEntity它空指針返回我的成功()

public class WeekEntity { 

    public HashMap<String, DayEntity> week; 

    public HashMap<String, DayEntity> getWeek() { 
     return week; 
    } 
} 



    public void getBusinessHours(final Context context) { 
      RestAdapter restAdapter = formatHeader(NetworkConstants.BUSINESS_HOURS_URL); 
      restAdapter.create(ApiService.class).getBusinessHours(new Callback<WeekEntity>() { 
       @Override 
       public void success(WeekEntity weekEntity, Response response) {     
        Log.v("~~~~~success", weekEntity.getWeek().toString()); 
       } 

       @Override 
       public void failure(RetrofitError error){     
       } 
      });  
    } 

回答

0
class WeekEntity{ 
    HashMap<String, DataEntity> week; 
    (...) 
} 

解析您的JSON這個UND你會得到一個地圖dayname -> DayEntity

+0

現在我有改造iteraction這樣,它作爲weekEntity所有的時間'@覆蓋 公共無效成功(WeekEntity weekEntity,響應響應)返回null {}' –

+0

您確定,您的服務器返回上面列出的JSON? –

0

GSON 2.x的是能夠改變可變密鑰的JSON在Map<String, DataEntity>

+0

請問您能否展示如何使用改造解析它?實際上,當正確的帶註釋的字段出現時,它會自動執行,現在我有點困惑如何做到這一點。 –

+0

你發佈的內容大多是正確的。不要使用'HashMap',使用'Map'代替 – Blackbelt

+0

哦,它再次給出空指針 –

0

您的代碼應該是這樣的。

public class DayEntity { 
    public CheckTime monday; 
    public CheckTime tuesday; 
    public CheckTime wednesday; 
    public CheckTime thursday; 
    public CheckTime friday; 
    public CheckTime saturday; 
    public CheckTime sunday; 
    public class CheckTime 
    { 
     private String open_time; 
     private String close_time; 
     //add your setter and getter here 
    } 
} 
2

最好是使用Gson Plugin for android studio將JSON字符串轉換爲InnerClassEntity。

here和安裝後在 文件/設置/插件

2)在安裝它的Android Studio中創建類,然後右鍵/生成/ GsonFormat並粘貼響應

1)下載插件並點擊確定。會自動生成響應對象(Json字符串)。保存並完成。

3)可以迭代在onResponse陣列然後這樣

public void onResponse(Response<ModelClass> response, Retrofit retrofit) { 

     for (int i=0;i<response.body().getResponse().getArray_name().size();i++) 
     { 
     Log.i("TAG", "retro array :" + response.body().getResponse().getArray_name().get(i).getItem()); 
     } 

} 

,當你intializing改型分配ConverterFactory這樣。

retrofit = new Retrofit.Builder() 
       .baseUrl(API_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 

的build.gradle

compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'