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

Aziiee

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!

dkb

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I iterate through JSON objects using Jackson?

Deserialize a json array to objects using Jackson and WebClient

Deserialize JSON array in a deserialized JSON array using Jackson

How to deserialize a JSON array of objects using Gson library?

How do I deserialize timestamps that are in seconds with Jackson?

How do I deserialize to Boolean.class from Json object in case insensitive manner using Jackson?

How do I deserialize a nested JSON array using GSON?

How to serialize and deserialize a list of objects using Jackson

How do I deserialize a JSON array that contains only values?

How do I deserialize object variables using Jackson?

How do I deserialize a JSON array using Newtonsoft.Json

How do I deserialize JSON into a List<SomeType> with Kotlin + Jackson

Deserialize JSON array to Swift objects using SwiftyJSON

How do I deserialize an array of JSON objects to a C# anonymous type?

How do I search an array of objects for any matches containing a string?

How do I deserialize a property containing an escaped JSON string?

How do I deserialize a json with nested array in c# using Newtonsoft.Json

How to deserialize JSON containing an array of objects with a single property name and value each into a c# model?

How do I, using XStream, serialize/deserialize objects in a type hierarchy?

How do I deserialize an array with indexes using jackson

How to deserialize a JSON array to a singly linked list by using Jackson

Deserialize JSON array to Map using Jackson

How do I deserialize an array of JSON data into a POJO using an abstract class?

Unable to deserialize Json that contain 2 objects with the same ID using jackson

How do I deserialize a JSON array without needing a wrapper type?

How do I make my php code correctly output a JSON Array containing objects and arrays

C# Json.Net - How to deserialize an array of objects containing an array of other objects

In C#.NET, how do I deserialize a JSON object that is not an array of objects, and has a unique name for every object?

How do I use Jackson Databind to deserialize the following JSON?