0

I have response from an API that looks like this,

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

I am using retrofit to make a request using the RxJava method. However, Gson doesn't seem to recognize the nested parts of my data and put it in the appropriate Model.

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

My Models look like this

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

    }
}

I also have RouteSchedule class model and other class models. However, after the normal response from retrofit using a single, my RouteSchedule model is empty.

Is there a way I can get route-schedule array alone or make sure it doesn't return an empty model.

2
  • So what happens when you construct this object just using: Gson.fromJson(yourResponseString, StopSchedule.class) (syntax might be off slightly - from memory)? Same can be done for the nested object, just parse the relevant section of the Json string. Commented Feb 6, 2018 at 23:56
  • add your pojo classes Commented Feb 7, 2018 at 2:06

1 Answer 1

1

your problem is in here.

@Expose
private List<RouteSchedule> mRouteSchedules;

@Expose is used to use the same fields names which is different from your JSON.

so remove the @Expose.

@SerializedName("route-schedules")
private List<RouteSchedule> mRouteSchedules;
Sign up to request clarification or add additional context in comments.

1 Comment

use gson for this my friend

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.