java - 使用 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 pojo和pojo sodhana 库之类的网站,但它们几乎可以做我所做的事情。
我对 JSON 和 Rest API 很陌生,如果很明显,我很抱歉。
解决方案
相反Route
,根对象中的类使用Map
:
private Map<String, Waypoint> route;
推荐阅读
- python - 使用 Python 在 Redis 中获取多个键/值
- javascript - 保存并读取数据库中的复选框值,更改标签的状态
- excel - #价值!用户编写的时间加权平均函数返回的错误
- java - 连接到远程 Mysql 数据库关闭
- scala - 获取排除包的列表
- python - 在Python中将函数绑定到类属性
- dependencies - 无法附加模块信息的依赖项
- yii - Yii1 - 防止分页(dataProvider)计数,或缓存它
- android - 无法获取通知数据,当应用程序处于后台或在 React-native-firebase android 中关闭时正文
- cd-rom - CIRC 编码器中的 CDROM、F2 Reed-Solomon PQ 码