改造具有不同对象的数组

Retrofit get array with different objects

我想发出 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 但不知道如何使用我的星期一、星期二等? 服务 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 可以很好地解析它,如何将它与 Retrofit 组合使用?我有 WeekEntity,它 returns 我 NullPointer 成功了()

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){                   
                }
            });      
    }
class WeekEntity{
    HashMap<String, DataEntity> week;
    (...)
}

将你的 json 解析成这个,你会得到一张地图 dayname -> DayEntity

Gson 2.x 能够用 Map<String, DataEntity>

中的可变键转换 JSON

你的代码应该是这样的。

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
   }
}

最好使用 android studio 的 Gson 插件将 JSON 字符串转换为 InnerClassEntity。

1) 从 here 下载插件并将其安装到 File/setting/plugin

的 android studio 中

2) 安装后,创建 class 然后 右键 click/Generate/GsonFormat 并粘贴您的回复并单击确定。将自动生成您的响应对象(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());
        }

}

并且当你像这样初始化改造分配 ConverterFactory

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

并在 build.gradle

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