英文:
How to unmarshall a ReponseEntity Object returned by RestTemplate?
问题
以下是您要翻译的内容:
我有两个第三方API,我正在访问并使用RestTemplate获得ResponseEntity<Object>
作为响应。我想要将响应解组为对象,以便我可以访问其中的内容。
ResponseEntity<Object> response = restTemplate.exchange(apiEndPointToHit, HttpMethod.GET, null, Object.class);
第一个API的响应
{
"responseStatus": "SUCCESS",
"size": 88,
"start": 0,
"limit": 200,
"sort": "id asc",
"users": [
{
"user": {
"user_name": "XYZ",
"user_first_name": "XYZ",
"user_last_name": "XYZ",
"user_email": "XYZ"
}
},
{
"user": {
"user_name": "ABC",
"user_first_name": "ABC",
"user_last_name": "ABC"
}
},
{
"user": {
"user_name": "PQR",
"user_first_name": "PQR",
"user_last_name": "PQR"
}
}
]
}
在这个响应中,"users" 将包含多个用户。我想要获取所有用户的列表,以便我可以访问列表中每个用户对象的字段,如 user_name、user_first_name 等。
第二个API的响应是
{
"message": {
"documents": {}
}
}
在这种情况下,一个消息将有一个文档。我想要访问这个文档。
JSON响应包含多个字段。因此,不可能创建一个POJO类。有没有办法在不创建POJO类的情况下访问响应实体中的对象?
英文:
I have two third party API's which I'm hitting and getting the response as ResponseEntity<Object>
using RestTemplate. I want to unmarshal the response into objects so that I can access the content inside of it.
ResponseEntity<Object> response = restTemplate.exchange(apiEndPointToHit,HttpMethod.GET,null,Object.class);
1st API Response
{
"responseStatus": "SUCCESS",
"size": 88,
"start": 0,
"limit": 200,
"sort": "id asc",
"users": [
{
"user": {
"user_name": "XYZ",
"user_first_name": "XYZ",
"user_last_name: "XYZ",
"user_email": "XYZ",
}
},
{
"user":{
"user_name":"ABC",
"user_first_name":"ABC",
"user_last_name":"ABC",
},
},
{
"user":{
"user_name":"PQR",
"user_first_name":"PQR",
"user_last_name":"PQR",
}
},
}
]
}
In this response, users will contain multiple user. I want to get list of all user. So that I can access the content of each user object inside that list fields like user_name, user_first_name, etc.
2nd API response is
{
"message": {
"documents": {
}
}
}
In this case, one message will have one document. I want to access the document.
JSON responses contain multiple fields. So it is not possible to create a POJO class.
Is there any way to access the Objects inside response entity with creating POJO classes?
答案1
得分: 1
我们可以使用对象映射器(Object Mapper)或 JSONObject 来帮助反序列化 JSON 主体。我建议使用 JSONObject,并按照以下示例来对 jsonbody 进行拆分。
1. 添加依赖
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
- 初始化 JSONObject,将 jsonResponse 传递给它。
JSONObject jo = new JSONObject(jsonResponse);
- 通过以下方式尝试访问 jsonResponse 中的对象
getJsonObject/getJsonArray
JSONArray ja = jo.getJsonArray("users");
英文:
We can use either Object mapper or JSONObject which helps to deserialize the JSON body. I would suggest to use JSONObject and following the below example to unmarshall jsonbody.
1. Add Dependency
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
- Initialize the JSONObject passing the jsonReponse from the restTemplate.
JSONObject jo = new JSONObject(jsonResponse);
- Try acceessing the objects in jsonResponse by just doing
getJsonObject/getJsonArray
JSONArray ja = jo.getJsonArray("users");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论