我有来自API的回复,
{
"stop-schedule": {
"stop": {
"key": 10064,
"name": "Northbound Osborne at Glasgow",
"number": 10064,
"direction": "Northbound",
"side": "Nearside",
"street": {
"key": 2715,
"name": "Osborne Street",
"type": "Street"
},
"cross-street": {
"key": 1486,
"name": "Glasgow Avenue",
"type": "Avenue"
},
"centre": {
"utm": {
"zone": "14U",
"x": 633838,
"y": 5525742
},
"geographic": {
"latitude": "49.86912",
"longitude": "-97.1375"
}
}
},
"route-schedules": [
{
"route": {
"key": 16,
"number": 16,
"name": "Route 16 Selkirk-Osborne",
"customer-type": "regular",
"coverage": "regular"
},
"scheduled-stops": [
{
"key": "8131173-23",
"times": {
"arrival": {
"scheduled": "2018-02-02T23:07:20",
"estimated": "2018-02-02T23:07:20"
},
"departure": {
"scheduled": "2018-02-02T23:07:20",
"estimated": "2018-02-02T23:07:20"
}
},
"variant": {
"key": "16-0-B",
"name": "Selkirk-Osborne to Tyndall Park via Burrows"
},
"bus": {
"bike-rack": "false",
"easy-access": "true",
"wifi": "false"
}
},
{
"key": "8131174-24",
"times": {
"arrival": {
"scheduled": "2018-02-02T23:32:20",
"estimated": "2018-02-02T23:32:20"
},
"departure": {
"scheduled": "2018-02-02T23:32:20",
"estimated": "2018-02-02T23:32:20"
}
},
"variant": {
"key": "16-0-M",
"name": "Selkirk-Osborne to Tyndall Park via Manitoba"
},
"bus": {
"bike-rack": "false",
"easy-access": "true",
"wifi": "false"
}
},
{
"key": "8131175-23",
"times": {
"arrival": {
"scheduled": "2018-02-02T23:57:20",
"estimated": "2018-02-02T23:57:20"
},
"departure": {
"scheduled": "2018-02-02T23:57:20",
"estimated": "2018-02-02T23:57:20"
}
},
"variant": {
"key": "16-0-B",
"name": "Selkirk-Osborne to Tyndall Park via Burrows"
},
"bus": {
"bike-rack": "false",
"easy-access": "true",
"wifi": "false"
}
},
{
"key": "8131176-24",
"times": {
"arrival": {
"scheduled": "2018-02-03T00:22:20",
"estimated": "2018-02-03T00:22:20"
},
"departure": {
"scheduled": "2018-02-03T00:22:20",
"estimated": "2018-02-03T00:22:20"
}
},
"variant": {
"key": "16-0-M",
"name": "Selkirk-Osborne to Tyndall Park via Manitoba"
},
"bus": {
"bike-rack": "false",
"easy-access": "true",
"wifi": "false"
}
}
]
}
]
},
"query-time": "2018-02-02T22:48:55"
}
我正在使用rxJava方法进行改造以发出请求。但是,Gson似乎无法识别我数据的嵌套部分并将其放入适当的模型中。
Retrofit retrofit= new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
我的模特看起来像这样
public class StopSchedule implements Serializable {
@SerializedName("query-time")
private String mQueryTime;
@SerializedName("route-schedules")
@Expose
private List<RouteSchedule> mRouteSchedules;
@SerializedName("stop")
private Stop mStop;
@SerializedName("stop-schedule")
private StopSchedule mStopSchedule;
public String getQueryTime() {
return mQueryTime;
}
public List<RouteSchedule> getRouteSchedules() {
return mRouteSchedules;
}
public Stop getStop() {
return mStop;
}
public StopSchedule getStopSchedule() {
return mStopSchedule;
}
public static class Builder {
private String mQueryTime;
private List<RouteSchedule> mRouteSchedules;
private Stop mStop;
private StopSchedule mStopSchedule;
public StopSchedule.Builder withQueryTime(String queryTime) {
mQueryTime = queryTime;
return this;
}
public StopSchedule.Builder withRouteSchedules(List<RouteSchedule> routeSchedules) {
mRouteSchedules = routeSchedules;
return this;
}
public StopSchedule.Builder withStop(Stop stop) {
mStop = stop;
return this;
}
public StopSchedule.Builder withStopSchedule(StopSchedule stopSchedule) {
mStopSchedule = stopSchedule;
return this;
}
public StopSchedule build() {
StopSchedule StopSchedule = new StopSchedule();
StopSchedule.mQueryTime = mQueryTime;
StopSchedule.mRouteSchedules = mRouteSchedules;
StopSchedule.mStop = mStop;
StopSchedule.mStopSchedule = mStopSchedule;
return StopSchedule;
}
}
}
我还有RouteSchedule
班级模型和其他班级模型。但是,在使用单个改造进行改造的正常响应之后,我的RouteSchedule模型为空。
有没有办法可以单独获取路线计划数组,或确保它不会返回空模型。
答案 0 :(得分:1)
你的问题就在这里。
@Expose
private List<RouteSchedule> mRouteSchedules;
@Expose
用于使用与您的JSON不同的相同字段名称。
删除@Expose
。
@SerializedName("route-schedules")
private List<RouteSchedule> mRouteSchedules;