📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Dependencies
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
- jackson-annotations-2.9.8.jar
- jackson-core-2.9.8.jar
- jackson-databind-2.9.8.jar
Converting JSON to Java Object Using ObjectMapper
Post.java
package net.javaguides.jackson.pojotojson;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
public class Post {
private Long id;
private String title;
private String description;
private String content;
private Date postedAt = new Date();
private Date lastUpdatedAt = new Date();
private Set < Tag > tags = new HashSet < > ();
public Post() {
}
public Post(String title, String description, String content) {
this.title = title;
this.description = description;
this.content = content;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
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 getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getPostedAt() {
return postedAt;
}
public void setPostedAt(Date postedAt) {
this.postedAt = postedAt;
}
public Date getLastUpdatedAt() {
return lastUpdatedAt;
}
public void setLastUpdatedAt(Date lastUpdatedAt) {
this.lastUpdatedAt = lastUpdatedAt;
}
public Set < Tag > getTags() {
return tags;
}
public void setTags(Set < Tag > tags) {
this.tags = tags;
}
}
Tag.java
package net.javaguides.jackson.pojotojson;
public class Tag {
private Long id;
private String name;
public Tag() {
}
public Tag(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public Tag(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Convert JSON to Java
package net.javaguides.jackson.jsontopojo;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.javaguides.jackson.pojotojson.Post;
import net.javaguides.jackson.pojotojson.Tag;
/**
* Convert JSON to Java object using Jackson
* @author Ramesh Fadatare
*
*/
public class JacksonJsonToPojo {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
// Read JSON file and convert to java object
InputStream fileInputStream = new FileInputStream("post.json");
Post post = mapper.readValue(fileInputStream, Post.class);
fileInputStream.close();
// print post object
System.out.println("Printing post details");
System.out.println(post.getId());
System.out.println(post.getTitle());
System.out.println(post.getDescription());
System.out.println(post.getContent());
System.out.println(post.getLastUpdatedAt());
System.out.println(post.getPostedAt());
// print tags of this post
System.out.println("Printing tag details of post :: " + post.getTitle());
for (Iterator < Tag > iterator = post.getTags().iterator(); iterator.hasNext();) {
Tag tag = (Tag) iterator.next();
System.out.println(tag.getId());
System.out.println(tag.getName());
}
}
}
Printing post details
100
Jackson JSON API Guide
Post about Jackson JSON API
HTML content here
Tue Apr 23 18:51:08 IST 2019
Tue Apr 23 18:51:08 IST 2019
Printing tag details of post :: Jackson JSON API Guide
2
Jackson
1
Java
3
JSON
Comments
Post a Comment
Leave Comment