Wednesday 16, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 12 October 2013

How to convert Object to JSON using Jackson


In this Tutorials we are going to see how to convert Java object to JSON String using Jackson data binding.
Jackson
Jackson is high performance JSON processor library available in Java.
1. Create project called JSONExample.
2. Create package called com.javatutorialscorner.jackson  3. Create java class called JacksonPojo under com.javatutorialscorner.jackson
JacksonPojo.java
01.package com.javatutorialscorner.jackson;
02. 
03.import java.util.List;
04. 
05.public class JacksonPojo {
06. private int id;
07. private String name;
08. private List<String> list;
09. 
10. public int getId() {
11.  return id;
12. }
13. 
14. public void setId(int id) {
15.  this.id = id;
16. }
17. 
18. public String getName() {
19.  return name;
20. }
21. 
22. public void setName(String name) {
23.  this.name = name;
24. }
25. 
26. public List<String> getList() {
27.  return list;
28. }
29. 
30. public void setList(List<String> list) {
31.  this.list = list;
32. }
33. 
34.}

4. add jackson-mapper-asl-1.9.13.jar, jackson-core-asl-1.9.13.jar into build path.

5. Now Create java class called JacksonWriteExample under com.javatutorialscorner.jackson

JacksonWriteExample.java

01.package com.javatutorialscorner.jackson;
02. 
03.import java.io.File;
04.import java.io.IOException;
05.import java.util.ArrayList;
06.import java.util.List;
07. 
08.import org.codehaus.jackson.JsonGenerationException;
09.import org.codehaus.jackson.map.JsonMappingException;
10.import org.codehaus.jackson.map.ObjectMapper;
11. 
12.public class JacksonWriteExample {
13. 
14. /**
15.  * @param args
16.  */
17. public static void main(String[] args) {
18.  // TODO Auto-generated method stub
19.  JacksonPojo pojo = null;
20.  ObjectMapper mapper = null;
21.  List<String> list = null;
22.  try {
23.   pojo = new JacksonPojo();
24.   pojo.setId(10);
25.   pojo.setName("Tendulkar");
26.   list = new ArrayList<String>();
27.   list.add("Test - 198 ");
28.   list.add("ODI - 463 ");
29.   list.add("T20 - 1");
30.   list.add("IPL - 78");
31.   pojo.setList(list);
32.   mapper = new ObjectMapper();
33.   mapper.writeValue(new File("C:\\jtc\\javatutorialscorner.json"),
34.     pojo);
35.   System.out.println("JSON String - "
36.     + mapper.writeValueAsString(pojo));
37.  } catch (JsonGenerationException e) {
38.   // TODO Auto-generated catch block
39.   e.printStackTrace();
40.  } catch (JsonMappingException e) {
41.   // TODO Auto-generated catch block
42.   e.printStackTrace();
43.  } catch (IOException e) {
44.   // TODO Auto-generated catch block
45.   e.printStackTrace();
46.  }
47. 
48. }
49. 
50.}

The writeValue() method used to write JSON String into file and writeValueAsString() method will return JSON String.

Now run the program see the following output in console and also JSON File created at specified location.

JSON String - {"id":10,"name":"Tendulkar","list":["Test - 198 ","ODI - 463 ","T20 - 1","IPL - 78"]}

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • JSON Tree Model in JacksonIn this Tutorials we are going to see how to read and write JSON Tree model using Jackson JsonNode. The JsonNode is used to read and write tree structured date.It is similar to XML Dom tree. 1. Create project called JSONE… Read More
  • Jackson Streaming API to Write JSONIn this tutorial we are going to see how to write JSON into file using JsonGenerator class available in Jackson.Jackson Streaming API is high-performance Java API for read write JSON. JsonGenerator – is used to write JSON a… Read More
  • Create Map to JSON using JacksonIn this tutorial we are going to see how to create JSON from map and print json as string and also write json in file 1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.jackson  … Read More
  • How to Create Pretty Print JSON using JacksonIn this Tutorials we are going to see how to create Pretty Print JSON using Jackson. 1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.jackson  3. Create java class called… Read More
  • JSON to Map using JacksonIn this tutorial we are going to see how to convert JSON into Map and retrieve the values   1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.jackson  3. add jackson-map… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to convert Object to JSON using Jackson Rating: 5 Reviewed By: eHowToNow