首页 文章

Retrofit和RxJava无法正确序列化Json Data

提问于
浏览
0

我有一个看似这样的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模型为空 .

有没有办法我可以单独获得route-schedule数组或确保它不返回空模型 .

1 回答

  • 1

    你的问题就在这里 .

    @Expose
    private List<RouteSchedule> mRouteSchedules;
    

    @Expose 用于使用与JSON不同的相同字段名称 .

    所以删除 @Expose .

    @SerializedName("route-schedules")
    private List<RouteSchedule> mRouteSchedules;
    

相关问题