如何使用 jackson 反序列化包含对象数组的 JSON 表示?

How do I deserialize a JSON representation containing array of objects using jackson?

我有与此处完全相同的 JSON 表示:https://newsapi.org/docs/endpoints/top-headlines

为了将其反序列化为 java 个对象,我创建了一个 News 和一个 Article class。 News 包含多个 Articles。所以这是我的 classes:

新闻:

public class News {

private String status;

private int totalResults;

private Article[] articles;

public News() {
}

public News(String status, int totalResults, Article[] articles) {
    this.status = status;
    this.totalResults = totalResults;
    this.articles = articles;
}

public String getStatus() {
    return status;
}

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

public int getTotalResults() {
    return totalResults;
}

public void setTotalResults(int totalResults) {
    this.totalResults = totalResults;
}

public Article[] getArticles() {
    return articles;
}

public void setArticles(Article[] articles) {
    this.articles = articles;
}

}

文章:

public class Article {

private String source;

private String author;

private String title;

private String description;

private String url;

private String imageUrl;

private String publishedAt;

private String content;

public Article() {
}

public Article(String source, String author, String title, String description, String url, String imageUrl,
        String publishedAt, String content) {
    this.source = source;
    this.author = author;
    this.title = title;
    this.description = description;
    this.url = url;
    this.imageUrl = imageUrl;
    this.publishedAt = publishedAt;
    this.content = content;
}

public String getSource() {
    return source;
}

public void setSource(String source) {
    this.source = source;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getImageUrl() {
    return imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

public String getPublishedAt() {
    return publishedAt;
}

public void setPublishedAt(String publishedAt) {
    this.publishedAt = publishedAt;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

}

现在我正在使用 com.fasterxml.jackson.databind.ObjectMapper 将 JSON 表示反序列化为 News 对象:

ObjectMapper objectMapper = new ObjectMapper();
News news = objectMapper.readValue(response.toString(), News.class);

这里我得到一个 com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token

问题显然是 JSON 中表示的文章数组。 我已经阅读了 jackson 中的数组反序列化,但我没有发现任何关于包含属性和对象数组的对象的反序列化。 https://www.baeldung.com/jackson-deserialization

如何使用 ObjectMapper 正确执行此操作?我错过了什么吗?感谢任何帮助,谢谢!

你的源码映射有误, source 字段的格式为

source": {
"id": "google-news",
"name": "Google News"
}

这可以替换为

public class Source {
 private String id;
 private String name;
 
 public Source() {}
 public Source(String id, String name) {
  this.id = id;
  this.name = name;
 }
}

并替换

private String source;

private Source source;

Articleclass