java获取内嵌json数组_java - 通过Spring restTemplate从嵌套的JSON对象获取数组作为列表 - 堆栈内存溢出...

我有一个获得JSON的REST HTTPGET答案。 它是Here Map API创建的答案:

{

"response": {

"metaInfo": {

"timestamp": "2019-05-22T15:56:28Z",

"mapVersion": "8.30.96.157",

"moduleVersion": "7.2.201920-3525",

"interfaceVersion": "2.6.56",

"availableMapVersion": [

"8.30.96.157"

]

},

"route": [

{

"waypoint": [

],

"mode": {

"type": "fastest",

"transportModes": [

"car"

],

"trafficMode": "disabled",

"feature": []

},

"leg": [

{

"start": {x

},

"end": {y

},

"length": 43301,

"travelTime": 2191,

"maneuver": [

{

"position": {

"latitude": ,

"longitude":

},

"instruction": "bla",

"travelTime": 74,

"length": 387,

"id": "M1",

"_type": "PrivateTransportManeuverType"

},

{

"position": {

"latitude": ,

"longitude":

},

"instruction": "",

"travelTime": 25,

"length": 12,

"id": "M2",

"_type": "PrivateTransportManeuverType"

},

{

"position": {

"latitude": ,

"longitude":

},

"instruction": "",

"travelTime": 53,

"length": 515,

"id": "M3",

"_type": "PrivateTransportManeuverType"

},

{

"position": {

"latitude": ,

"longitude":

},

"instruction": "",

"travelTime": 59,

"length": 309,

"id": "M4",

"_type": "PrivateTransportManeuverType"

},

{

"position": {

"latitude": ,

"longitude":

},

"instruction": "Arrive at target",

"travelTime": 0,

"length": 0,

"id": "M19",

"_type": "PrivateTransportManeuverType"

}

]

}

],

"summary": {

"distance": 43301,

"trafficTime": 2648,

"baseTime": 2191,

"flags": [

"tunnel",

"motorway",

"builtUpArea"

],

"text": "",

"travelTime": 2191,

"_type": "RouteSummaryType"

}

}

],

"language": "en-us"

}

}

(出于隐私和长度的原因,我剥离了15个航点和坐标)

我想通过springboot应用程序和restTemplate将嵌套数组“maneuver”作为排序列表。

我知道如何到达对象。 我已经使用过JSON2POJO映射器并且可以使用所有类。 同

@Bean

public CommandLineRunner run(RestTemplate restTemplate) throws Exception {

return args -> {

ResponseEntity responseEntity = restTemplate

.getForEntity("https://route.api.here.com/routing/7.2/calculateroute.json?app_id=xjipTEag66SG6fNwgEQm&app_code=0eiHle7fs2IZsXChP2sWKQ&waypoint0=geo!" + start + "&waypoint1=geo!" + end + "&mode=fastest;car;traffic:disabled", Response[].class);

Response[] objects = responseEntity.getBody();

HttpStatus statusCode = responseEntity.getStatusCode();

log.info("Status" + statusCode);

Arrays.asList(objects).forEach(maneuver -> log.info(maneuver.toString()));

};

}

给我一个反序列化错误。

@Bean

public CommandLineRunner run(RestTemplate restTemplate) throws Exception {

return args -> {

Response response = restTemplate.getForObject(

"https://route.api.here.com/routing/7.2/calculateroute.json?app_id=xjipTEag66SG6fNwgEQm&app_code=0eiHle7fs2IZsXChP2sWKQ&waypoint0=geo!" + start + "&waypoint1=geo!" + end + "&mode=fastest;car;traffic:disabled", Response.class);

log.info(response.toString());

};

我可以读出像“ID”这样的单个对象,并将它们列为一个字符串:

2019-05-22 19:09:34.253 INFO 15928 --- [ main] trackr.GetRest : No active profile set, falling back to default profiles: default

2019-05-22 19:09:34.636 INFO 15928 --- [ main] trackr.GetRest : Started GetRest in 0.561 seconds (JVM running for 1.016)

2019-05-22 19:09:35.067 INFO 15928 --- [ main] klib.trackr.GetRest : [[[M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M11, M12, M13, M14, M15, M16, M17, M18, M19]]]

我对任何建议感到高兴。 我是Java和Spring restTemplate的新手,我尝试了在StackOverflow上找到的不同解决方案,但要么我使用它们错了,要么我不理解它。 非常感谢任何建议。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值