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

JSON Streaming API to Read JSON


In 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 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 JSONParserExample under com.javatutorialscorner.jackson
JSONParserExample.java
package com.javatutorialscorner.jackson;

import java.io.File;
import java.io.IOException;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

public class JSONParserExample {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JsonFactory factory = null;
JsonParser jsonParser = null;

try {
factory = new JsonFactory();
jsonParser = factory.createJsonParser(new File(
"C:\\jtc\\javatutorialscorner.json"));
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {

String field = jsonParser.getCurrentName();
if ("name".equals(field)) {
jsonParser.nextToken();
System.out.println("Name : " + jsonParser.getText());
} else if ("rank".equals(field)) {
jsonParser.nextToken();
System.out.println("Rank : " + jsonParser.getIntValue());
} else if ("Tutorials".equals(field)) {
jsonParser.nextToken();
while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
System.out.println("Tutorials : "
+ jsonParser.getText());
}
}
}
jsonParser.close();
} catch (JsonParseException 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.

Name : Java Tutorials Corner

Rank : 1


Tutorials : Java


Tutorials : Servlet


Tutorials : JSP


Tutorials : Struts 1.x


Tutorials : Struts 2.x


Tutorials : Spring


Tutorials : Hibernate


Tutorials : Log4j


Tutorials : JSON


Tutorials : Web Services


Tutorials : JSF


Tutorials : Eclipse

Shop and help us

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

Related Posts:

  • 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 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
  • 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 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 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
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: JSON Streaming API to Read JSON Rating: 5 Reviewed By: eHowToNow