0

I have the exact same JSON representation as here: https://newsapi.org/docs/endpoints/top-headlines

To deserialize this into java objects I have created a News and a Article class. News contains multiple Articles. So here are my classes:

News:

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;
}

}

Article:

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;
}

}

Now I am using the com.fasterxml.jackson.databind.ObjectMapper as following to deserialize the JSON representation into a News object:

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

Here I am getting a com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token

The problem apparently is the array of articles representated in JSON. I have read about arrays deserialization in jackson but I found nothing about deserialization of objects that contain properties AND array of objects. https://www.baeldung.com/jackson-deserialization

How do I do this properly using the ObjectMapper? Am I missing out on something? Any help is appreciated, thanks!

1
  • 1
    In Java, always prefer List to native arrays when practical. (Jackson can map to/from both of them with no problem.) Commented Nov 16, 2020 at 4:08

1 Answer 1

3

Your source mapping is wrong, the source field is of format

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

this can be replaced with

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

and replace

private String source;

with

private Source source;

in the Article class

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.