如何使用java中的Jackson Annotation创建一个包含数组的JSON请求主体对象

问题描述 投票:0回答:1

目标: 想要为以下 JSON 创建 Json 请求正文:

[
  {
    "text": "Hey There, its me!"
  }
]

尝试过的解决方法:

public class JsonBodyInsideTheArray {

private final String Text;

    @JsonCreator
    public JsonBodyInsideTheArray (@JsonProperty("Text") String Text) {
        this.Text = Text;
    }

    public String getText() {
        return Text;
    }
}

数组外:

public class JsonBodyAboveTheArray {

    private List<JsonBodyInsideTheArray> requests;

    @JsonCreator
    public JsonBodyAboveTheArray (@JsonProperty("") List<JsonBodyInsideTheArray > requests) {
        this.requests = requests;
    }

    public List<JsonBodyInsideTheArray > getRequests() {
        return requests;
    }
}

使用上面的代码,会检索到以下 JSON,这不是预期的: `

{
  "requests": [
    {
      "text": "Hey There, its me!"
    }
  ]
}

`

java json jackson
1个回答
0
投票

List<JsonBodyInsideTheArray>
是你期望的输出

© www.soinside.com 2019 - 2024. All rights reserved.