【发布时间】:2015-07-23 05:27:40
【问题描述】:
我目前正在消费风格的内容
"potter",["potter harry","potter","potter hallows 2","potter collection","potter hallows 1","potter dvd box","potter 7","potter hallows","potter blue ray","potter feniks"],[{"xcats":[{"name":"dvd_all"},{"name":"books_nl"}]},{},{},{},{},{},{},{},{},{}],[]]
在 Spring 中使用以下代码
RestTemplate restTemplate = new RestTemplate();
String information = restTemplate.getForObject(URL, String.class);
//further parsing of information, using String utilities
显然这不是要走的路,因为我应该能够以某种方式自动解析它。我也只需要第二个元素的内容(数组,从potter harry 到potter feniks)。
当它的 json 内容不是名称值时,解析这样的 GET 响应的最佳方法是什么?
【问题讨论】:
-
首先,消费内容的 JSON 格式也不是很好,在 rest 模板中你可以传递你想要的对象,如果 spring rest 模板有合适的消息转换器,它将“自动”解析响应。例如:
ResponseEntity<Map<String,AdInsightDataMixin>> entity = getTemplate().exchange(builder.toUri(), HttpMethod.GET, null, new ParameterizedTypeReference<Map<String, AdInsightDataMixin>>() { }); -
那么,我如何为这样的响应创建一个合适的消息转换器,它没有键?