RestTemplate 使用动态键映射 JSON 键值对对象 [英] RestTemplate map JSON key-value pair object with with dynamic keys
本文介绍了RestTemplate 使用动态键映射 JSON 键值对对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到了 JSON 键值对对象的响应,其中包含使用 Java Spring RestTemplate 完成的 HTTP 请求的动态键,如下所示.
I get a response of JSON key-value pair object with with dynamic keys for a HTTP request done using Java Spring RestTemplate as shown below.
回复:
{
"1234x": {
"id": "1234x",
"description": "bla bla",
...
},
"5678a": {
"id": "5678a",
"description": "bla bla bla",
...
},
...
}
如何将响应对象映射到 POJO 或 Map ?
How to map the response object to a POJO or a Map ?
我使用 RestTemplate 如下.
I am using RestTemplate as following.
RestTemplate restTemplate = new RestTemplate();
String url = "my url";
HttpHeaders headers = new HttpHeaders();
HttpEntity entity = new HttpEntity(headers);
response = restTemplate.exchange(url, HttpMethod.GET, entity, ???);
推荐答案
您可以简单地将 ParameterizedTypeReference 与 Map 一起使用(您可以根据您的用例进行自定义):
You can simply use ParameterizedTypeReference with Map (you can customize it according to your use case) :
response = restTemplate.exchange(url, HttpMethod.GET, entity, new ParameterizedTypeReference<Map<String, Object>>() {});
这篇关于RestTemplate 使用动态键映射 JSON 键值对对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文