Trips.json
{
"trips":[
{
"summary": {
"subTrips":3,
"changes":1,
"duration":"1:09",
"departure":{
"date":"2017-05-07",
"time":"11:46",
},
"arrival":{
"date":"2017-05-07",
"time":"12:55",
},
},
"subTrips":[
{
"departure":{
"date":"2017-05-07",
"time":"11:46",
},
"arrival":{
"date":"2017-05-07",
"time":"12:07",
},
"intermediateStopsKey":"c474e7c"
},
{
"departure":{},
"arrival":{},
"intermediateStopsKey":"c81e728"
}
]
},
{
"summary": {},
"subTrips": [
{ "intermediateStopsKey": "123456" }
]
}
]
}
IntermediateStops.json
{
"status": 1,
"data": [
{
"stationName": "Husby",
"arrivalTime": "19:59",
"departureTime": "19:59",
},
{
"stationName": "Kista",
"arrivalTime": "20:01",
"departureTime": "20:01"
},
{
"stationName": "Hallonbergen",
"arrivalTime": "20:05",
"departureTime": "20:05"
}
]
}
我的第一个API调用返回Trips.json
。 subTrips
包含多个intermediateStopsKey
字段。每个intermediateStopsKey
都是一个新的API调用,用于获取我需要注入IntermediateStops.json
的{{1}}。 Trips.json
似乎是这样做的方法,但我需要帮助如何链接命令以获得所需的结果。
flatMap()
请求被正确链接,我不确定如何将我的getIntermediateStops API调用的结果插入到它们各自的SubTripsModels中
答案 0 :(得分:0)
API().getTrips(params)
.observeOn(AndroidSchedulers.mainThread())
.doOnRequest((Long request) -> dummyView.setVisibility(View.VISIBLE))
.doOnNext(response -> getTrips(response.body().getData()))
.doOnCompleted(() -> createListView())
.flatMap(getTripsModel -> Observable.from(getTripsModel.body().getData().getTrips()))
.flatMap(tripsModel -> Observable.from(tripsModel.getSubTrips()))
.map((SubTripsModel subTripsModel) -> API().getIntermediateStops(
subTripsModel.getIntermediateStops(), Token.getInstance().TOKEN)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Response<BaseResponseModel<List<IntermediateStopsModel>>>>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Response<BaseResponseModel<List<IntermediateStopsModel>>> baseResponseModelResponse) {
subTripsModel.setIntermediateStation(baseResponseModelResponse.body().getData());
}
}))
.doOnCompleted(() -> dummyView.setVisibility(View.GONE))
.subscribe(finalResponse -> {
}, throwable -> {
Log.i("REST ERROR", RestErrorHelper.getErrorMessage(throwable, mContext));
});
这适用于任何有兴趣的人,您使用flatMap()
深入了解嵌套的JSON,当您到达可以迭代subTrips
对象数组的点时,您可以使用map()
功能并使用subscribe()
您可以选择迭代每个subTrip
并在那里进行链式调用。