简体   繁体   English

如何从json响应中获取特定对象

[英]How to get specific object from json response

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. object1, object2 和 object3 不是同一个类型,我只想从响应中获取 object2。

I have tried this approach:我试过这种方法:

ResponseEntity<ResponseClass> response = restTemplate.exchange( uri, HttpMethod.POST, entity, ResponseClass.class );

Where ResponseClass looks like this: 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;

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.我得到了完整的 objectList,所以我可以将它提取为键值对,如 objectList.get(0).get("object2") 或类似的东西。 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?任何人都可以就如何改进这一点或如何获得我想要的对象提供一些指导吗?

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.在您的示例中,您基本上也有一个包含 object1、object2 和 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.另一种方法是在 JsonNode 中有响应,然后如果您不想将其映射到自定义对象,则对其执行 getProperty() 。

objectList[0].object2

0 给你列表中的第一项然后 object2 访问字典的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM