我正在尝试使用 Java
和 GSON
反序列化来自 RouteXL 的响应,但是,路由作为单独的对象返回,并且不是一个很痛苦的数组。有谁知道反序列化 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 pojo 等网站和 pojo sodhana library但他们几乎做了我所做的事情。
我对 JSON 和 Rest API 还很陌生,所以如果显而易见,我深表歉意。
最佳答案
相反,根对象中的Route
类使用Map
:
private Map<String, Waypoint> route;
关于java - 使用 Gson 在 Java 中反序列化 RouteXL 响应很困难,因为 RouteXL 返回多个对象而不是数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58288229/