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

How to Create Pretty Print JSON using Jackson



In 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 JacksonPojo under com.javatutorialscorner.jackson
JacksonPojo.java

package com.javatutorialscorner.jackson;

import java.util.List;

public class JacksonPojo {
private int id;
private String name;
private List<String> list;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<String> getList() {
return list;
}

public void setList(List<String> list) {
this.list = list;
}

}

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

package com.javatutorialscorner.jackson;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JacksonWriteExample {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JacksonPojo pojo = null;
ObjectMapper mapper = null;
List<String> list = null;
try {
pojo = new JacksonPojo();
pojo.setId(10);
pojo.setName("Tendulkar");
list = new ArrayList<String>();
list.add("Test - 198 ");
list.add("ODI - 463 ");
list.add("T20 - 1");
list.add("IPL - 78");
pojo.setList(list);
mapper = new ObjectMapper();
mapper.defaultPrettyPrintingWriter().writeValue(new File("C:\\jtc\\javatutorialscorner.json"),
pojo);
System.out.println("JSON Pretty Print String - "
+ mapper.defaultPrettyPrintingWriter().writeValueAsString(pojo));
} catch (JsonGenerationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

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

JSON Pretty Print 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:

  • Count XML Element using DOM parserIn this tutorials we going to see how to count the element in XML document using DOM parser . 1. Create Project Called JavaXML. 2. Create package called com.javatutorialscorner.xml.dom under JavaXML. 3. Create Java class cal… Read More
  • JSON Streaming API to Read JSONIn this tutorial we are going to see how to read JSON from file using JsonParser class available in Jackson.Jackson Streaming API is high-performance Java API for read write JSON. JsonGenerator – is used to write JSON as St… Read More
  • Modify XML file using DOM ParserIn this tutorials we going to see how to modify (add new element, update attribute, update element, delete element )the existing XML document using DOM parser 1. Create Project Called JavaXML. 2. Create package called com.j… Read More
  • Java Regular Expression Syntax Descriptions 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
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to Create Pretty Print JSON using Jackson Rating: 5 Reviewed By: eHowToNow