Tuesday 15, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Friday, 18 October 2013

Jackson Streaming API to Write JSON


In 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 as String and File.
1. Create project called JSONExample.
2. Create package called com.javatutorialscorner.jackson 
3. add jackson-mapper-asl-1.9.13.jar, jackson-core-asl-1.9.13.jar into build path.
4. Create java class called JSONGeneratorExample under com.javatutorialscorner.jackson
JSONGeneratorExample.java
01.package com.javatutorialscorner.jackson;
02. 
03.import java.io.File;
04.import java.io.IOException;
05. 
06.import org.codehaus.jackson.JsonEncoding;
07.import org.codehaus.jackson.JsonFactory;
08.import org.codehaus.jackson.JsonGenerator;
09. 
10.public class JSONGeneratorExample {
11. 
12. /**
13.  * @param args
14.  */
15. public static void main(String[] args) {
16.  // TODO Auto-generated method stub
17.  JsonFactory factory = null;
18.  JsonGenerator jsonGenerator = null;
19. 
20.  try {
21.   factory = new JsonFactory();
22.   jsonGenerator = factory.createJsonGenerator(new File(
23.     "C:\\jtc\\javatutorialscorner.json"), JsonEncoding.UTF8);
24.   jsonGenerator.writeStartObject();
25.   jsonGenerator.writeStringField("name", "Java Tutorials Corner");
26.   jsonGenerator.writeNumberField("rank", 1);
27.   jsonGenerator.writeFieldName("Tutorials");
28.   jsonGenerator.writeStartArray();
29.   jsonGenerator.writeString("Java");
30.   jsonGenerator.writeString("Servlet");
31.   jsonGenerator.writeString("JSP");
32.   jsonGenerator.writeString("Struts 1.x");
33.   jsonGenerator.writeString("Struts 2.x");
34.   jsonGenerator.writeString("Spring");
35.   jsonGenerator.writeString("Hibernate");
36.   jsonGenerator.writeString("Log4j");
37.   jsonGenerator.writeString("JSON");
38.   jsonGenerator.writeString("Web Services");
39.   jsonGenerator.writeString("JSF");
40.   jsonGenerator.writeString("Eclipse");
41.   jsonGenerator.writeEndArray();
42.   jsonGenerator.writeEndObject();
43.   jsonGenerator.close();
44. 
45.  } catch (IOException e) {
46.   // TODO Auto-generated catch block
47.   e.printStackTrace();
48.  }
49. 
50. }
51. 
52.}

Now run the program see the file created at specified location with following content.

{"name":"Java Tutorials Corner","rank":1,"Tutorials":["Java","Servlet","JSP","Struts 1.x","Struts 2.x","Spring","Hibernate","Log4j","JSON","Web Services","JSF","Eclipse"]}

Shop and help us

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

Related Posts:

  • 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
  • How to convert Object to JSON using JacksonIn this Tutorials we are going to see how to convert Java object to JSON String using Jackson data binding. JacksonJackson is high performance JSON processor library available in Java. 1. Create project called JSONExample. … Read More
  • 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
  • How to convert JSON to Java Object using JacksonIn this Tutorials we are going to see how to convert JSON String to Java object using Jackson data binding. 1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.jackson  3. Cr… 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: Jackson Streaming API to Write JSON Rating: 5 Reviewed By: eHowToNow