Deserialising RouteXL Response In Java using Gson Difficult as RouteXL Returns Multiple Objects Rather Than Array

OutsideCoder :

I'm trying to deserialize the response from RouteXL using Java & GSON, however, the routes come back as separate objects, and not an array which is a pain. Does anyone know an easy way of deserializing the response from RouteXL API1? Sadly API2 does the same.

Json Response:

{
  "id": "xJFPS2Z3",
  "count": 4,
  "feasible": true,
  "route": {
    "0": {
      "name": "11 Main Street",
      "arrival": 0,
      "distance": 0
    },
    "1": {
      "name": "19 Slate Road",
      "arrival": 10,
      "distance": 9.3
    },
    "2": {
      "name": "234 Woodland Road",
      "arrival": 18,
      "distance": 14.5
    },
    "3": {
      "name": "15 Great Eastern Road",
      "arrival": 29,
      "distance": 26.4
    }
  }
}

Its almost like you need to create a class like this to hold all the responses:

public class Route {
    private Waypoint 1;
    private Waypoint 2;
    private Waypoint 3;
    private Waypoint 4;
    private Waypoint 5;
    ...

    public Route(Waypoint 1, Waypoint 2, Waypoint 3, Waypoint 4, Waypoint 5) {
        this.1 = 1;
        this.1 = 2;
        this.3 = 3;
        this.4 = 4;
        this.5 = 5;
    }
}

Which isn't valid java as the variable cannot be a number.

I've tried using sites such as json schema 2 pojo and pojo sodhana library but they pretty much do what I have done.

I'm pretty new to JSON and Rest API's so apologies if obvious.

Michał Ziober :

Instead Route class in root object use Map:

private Map<String, Waypoint> route;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=307104&siteId=1