Jackson – 解析嵌套的数组数组

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

Jackson - parse nested array of arrays

问题

以下是您要翻译的内容:

这里有一个JSON字符串:

{
  "status": 200,
  "result": [
    [
      123,
      "FOO",
      "BAR",
      456
    ],
    [
      123,
      "FOO",
      "BAR",
      456
    ]
  ]
}

我需要解析这个对象并获取status字段以及result JSON属性中的所有数组(可能会有1个或多个这样的数组)。

我使用了这个DTO:

@JsonIgnoreProperties(ignoreUnknown = true)
public class TestDto {
    @JsonProperty("status")
    private Integer status;

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }
}

来解析JSON,但我不知道如何解析result数组。

是否可能使用这样简单的DTO文件来完成,还是我需要创建自定义的com.fasterxml.jackson.databind.JsonDeserializer

英文:

There is this JSON string:

{
  "status": 200,
  "result": [
    [
      123,
      "FOO",
      "BAR",
      456
    ],
    [
      123,
      "FOO",
      "BAR",
      456
    ],
  ]
}

and I need to parse this object and get status field and all arrays in result JSON property. (There could be 1...n of these arrays.)

I use this DTO:

@JsonIgnoreProperties(ignoreUnknown = true)
public class TestDto {
    @JsonProperty("status")
    private Integer status;

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }
}

to parse JSON but I have no idea how to parse the result array.

Is it possible to accomplish it with such simple DTO file or do I need to create custom com.fasterxml.jackson.databind.JsonDeserializer?

答案1

得分: 2

你可以使用任何2维数组。

两者都可以完成任务:

private Object[][] result;

private List<List<Object>> result;

可以完成任务。

英文:

You can use anything 2-dimensional.

Both:

private Object[][] result;

and

private List&lt;List&lt;Object&gt;&gt; result;

can do the job.

huangapple
  • 本文由 发表于 2023年5月25日 23:07:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333794.html
匿名

发表评论

匿名网友

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

确定