首页 > 解决方案 > 使用 Gson 困难的 Java 反序列化 RouteXL 响应,因为 RouteXL 返回多个对象而不是数组

问题描述

Java我正在尝试使用&反序列化来自 RouteXL 的响应GSON,但是,路由作为单独的对象返回,而不是一个痛苦的数组。有谁知道反序列化响应的简单方法RouteXL API1?可悲的是 API2 也是如此。

json响应:

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

几乎就像您需要创建一个这样的类来保存所有响应:

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

这不是有效的java,因为变量不能是数字。

我尝试过使用诸如json schema 2 pojopojo sodhana 库之类的网站,但它们几乎可以做我所做的事情。

我对 JSON 和 Rest API 很陌生,如果很明显,我很抱歉。

标签: javajsonrestapigson

解决方案


相反Route,根对象中的类使用Map

private Map<String, Waypoint> route;

推荐阅读