【发布时间】:2018-02-06 23:51:56
【问题描述】:
我收到了类似这样的 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 模型是空的。
有没有办法我可以单独获取路由调度数组或确保它不返回空模型。
【问题讨论】:
-
那么,当您仅使用
Gson.fromJson(yourResponseString, StopSchedule.class)构造此对象时会发生什么(语法可能略有不同 - 从内存中)?嵌套对象也可以这样做,只需解析Json字符串的相关部分即可。 -
添加你的 pojo 类