将 JSON 转换为对象抛出 JsonMappingException "Can not deserialize instance of class out of START_ARRAY token"

Convert JSON to Object throws JsonMappingException "Can not deserialize instance of class out of START_ARRAY token"

我正在尝试使用他们的 REST API (link) 获取有关我的 Trello 板的信息,并将其保存在 Java 对象中。我按照最初的步骤和 运行 这个命令我得到了我想要的所有信息:

curl https://api.trello.com/1/members/me/boards?fields=name,url&key={apiKey}&token={apiToken}

其中 returns 数据格式如下:

[
  {
   "name": "Greatest Product Roadmap",
   "id": "5b6893f01cb3228998cf629e",
   "url": "https://trello.com/b/Fqd6NosI/greatest-product-roadmap"
  },
  {
    "name": "Never ending Backlog",
    "id": "5b689b3228998cf3f01c629e",
    "url": "https://trello.com/b/pLu77kV7/neverending-backlog"
  },
  //....
{

我创建了一个BoardClass

@JsonIgnoreProperties(ignoreUnknown = true)
public class Board {
    private String name;
    private String shortLink;
    private String idBoardSource;
    private String id;
    private String url;

    public Board() {
    }

    //getters, setters
}

在我的 servlet 中,我执行:

//Get the response
String command = "curl -X GET https://api.trello.com/1/members/me/boards?fields=name,url&key={apiKey}&token={apiToken}";
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
processBuilder.directory();
Process process = processBuilder.start();
InputStream inputStream = process.getInputStream();

//Convert the InputStream into String
String jsonString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);

//try to create an Board class
Board board = new ObjectMapper().readValue(jsonString, Board.class);

但是,我得到一个错误:

org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of gr.kotronis.trello.Board out of START_ARRAY token

我尝试并添加了 @JsonIgnoreProperties(ignoreUnknown = true),但错误仍然出现。我搜索了有关此错误的类似问题,但没有找到解决方案。 响应数据的格式是正确的,所以我不明白为什么会出现这个错误...

问题是您需要将 JSON 数组反序列化为 java 数组,或 java 列表。 在这里你可以找到答案 How to use Jackson to deserialise an array of objects.