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

huangapple go评论72阅读模式
英文:

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:

{
    &quot;field1&quot;: &quot;string&quot;,
    &quot;field2&quot;: &quot;string&quot;,
    &quot;objectList&quot;: [
        {
            &quot;object1&quot;: {
                &quot;field11&quot;: &quot;string&quot;,
                &quot;field12&quot;: &quot;string&quot;,
                &quot;field13&quot;: &quot;string&quot;,
                &quot;field14&quot;: &quot;string&quot;,
            },
            &quot;object2&quot;: {
                &quot;field21&quot;: &quot;string&quot;,
                &quot;field22&quot;: &quot;string&quot;,
                &quot;field23&quot;: &quot;string&quot;,
            },
            &quot;object3&quot;: {
                &quot;field31&quot;: &quot;string&quot;,
                &quot;field32&quot;: &quot;string&quot;,
                &quot;field33&quot;: &quot;string&quot;,
                &quot;field34&quot;: &quot;string&quot;,
                &quot;field35&quot;: &quot;string&quot;,
            }
        }
    ]
}

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&lt;ResponseClass&gt; 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(&quot;field1&quot;)
    private String field1;
    @JsonProperty(&quot;field2&quot;)
    private String field2;
    @JsonProperty(&quot;objectList&quot;)
    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(&quot;field1&quot;)
    private String field1;
    @JsonProperty(&quot;field2&quot;)
    private String field2;
    @JsonProperty(&quot;objectList&quot;)
    private List&lt;IntendedObject&gt; objectList;
 }


@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class IntendedObject {
    @JsonProperty(&quot;object2&quot;)
    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

huangapple
  • 本文由 发表于 2020年8月23日 11:13:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63543030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定