英文:
How to get specific object from json response
问题
我从第三方服务收到的响应如下所示:
{
"field1": "string",
"field2": "string",
"objectList": [
{
"object1": {
"field11": "string",
"field12": "string",
"field13": "string",
"field14": "string"
},
"object2": {
"field21": "string",
"field22": "string",
"field23": "string"
},
"object3": {
"field31": "string",
"field32": "string",
"field33": "string",
"field34": "string",
"field35": "string"
}
}
]
}
其中 object1、object2 和 object3 不是相同的类型,我只想从响应中获取 object2。
我尝试了以下方法:
ResponseEntity<ResponseClass> response = restTemplate.exchange(uri, HttpMethod.POST, entity, ResponseClass.class);
其中 ResponseClass 如下所示:
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class ResponseClass implements Serializable {
private static final long serialVersionUID = -4355652702566088304L;
@JsonProperty("field1")
private String field1;
@JsonProperty("field2")
private String field2;
@JsonProperty("objectList")
private List objectList;
}
我得到了完整的 objectList,因此我可以提取它作为键值对,例如 objectList.get(0).get("object2")
或类似的方式。但我不确定是否有更好的解决方案。
请问是否有人可以提供一些改进方法,或者如何仅获取我想要的 object2?
英文:
I'm receiving a response from one third party service which looks like this:
{
"field1": "string",
"field2": "string",
"objectList": [
{
"object1": {
"field11": "string",
"field12": "string",
"field13": "string",
"field14": "string",
},
"object2": {
"field21": "string",
"field22": "string",
"field23": "string",
},
"object3": {
"field31": "string",
"field32": "string",
"field33": "string",
"field34": "string",
"field35": "string",
}
}
]
}
object1, object2 and object3 are not the same type, and I just want to get the object2 from the response.
I have tried this approach:
ResponseEntity<ResponseClass> response = restTemplate.exchange( uri, HttpMethod.POST, entity, ResponseClass.class );
Where ResponseClass looks like this:
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class ResponseClass implements Serializable {
private static final long serialVersionUID = -4355652702566088304L;
@JsonProperty("field1")
private String field1;
@JsonProperty("field2")
private String field2;
@JsonProperty("objectList")
private List objectList;
And I'm getting the full objectList, so I can extract it as key value pairs like objectList.get(0).get("object2") or something like that. But I'm unsure if there is a better solution.
Can anyone please provide some guidance on how to improve this or how to get just the object I want?
答案1
得分: 2
有两种方式来看待它。
可扩展性和纯粹的面向对象方式:
您需要在您的一侧正确映射整个响应对象,而不是使用模糊的列表。数组应始终具有相同类型的对象。在您的示例中,基本上您有一个包含 object1、object2 和 object3 的包装对象列表。
因此,基本上您应该这样做:
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class ResponseClass implements Serializable {
private static final long serialVersionUID = -4355652702566088304L;
@JsonProperty("field1")
private String field1;
@JsonProperty("field2")
private String field2;
@JsonProperty("objectList")
private List<IntendedObject> objectList;
}
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class IntendedObject {
@JsonProperty("object2")
private Object2 object2;
}
另一种方法是将响应放在 JsonNode 中,然后在不想映射到自定义对象的情况下对其使用 getProperty()。
英文:
There are two ways to look at it.
Extensibility and pure object-oriented way:
You need to map the whole response object properly at your side rather than having an ambiguous List. An array should always have objects of same type. In your example too basically you have a list of wrapper object which hold object1, object2, and object3.
So, basically you should do something like this :
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class ResponseClass implements Serializable {
private static final long serialVersionUID = -4355652702566088304L;
@JsonProperty("field1")
private String field1;
@JsonProperty("field2")
private String field2;
@JsonProperty("objectList")
private List<IntendedObject> objectList;
}
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class IntendedObject {
@JsonProperty("object2")
private Object2 object2;
}
The other way would be to have the response in a JsonNode and then do getProperty() over it if you don't want to map it over to a custom object.
答案2
得分: 0
objectList[0].object2
0 给出了列表中的第一个项目
然后 object2 访问了字典的值
英文:
objectList[0].object2
0 gives you the first item in the list
Then object2 accesses the value of the dictionary
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论