Jackson Steaming API
Hello friends.hope you are fine .today i am going to demonstrate how to process json data.there are various API's available in market ,here i am going to demonstrate you Jackson Streaming API Code.
it is API used for processing json data.how ever it process the data/token in incremental manner. it treats individual token as a element.it is faster & convinient way of processing json data.
below sample code creates json file using Jackson Streaming API:
package org.demo;
import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
public class JacksonReader
{
public static void main(String []args) throws IOException
{
JsonFactory jfactory =new JsonFactory();
JsonGenerator jgenerator=jfactory.createJsonGenerator(new File("D:\\ocjp\\Data.json"),JsonEncoding.UTF8);//used to write data in streaming way
jgenerator.writeStartObject();//{
jgenerator.writeStringField("Name","Akshita");//"Name":Akshita
jgenerator.writeNumberField("Age",21);//"Age":21
jgenerator.writeString("Reading");//"Reading"
jgenerator.writeString("Coding");//"Coding"
jgenerator.writeString("Surfing");://"Surfing"
jgenerator.writeEndArray(); //]
jgenerator.close();//close the document
}
}
Sample data.json file that will be generated after compiliation of the above code:
{ "Name":Akshita
"Age":21 ,
"hobby":["Reading","Coding","Surfing"]
};
below code show how to read json data from json file:
import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.JsonMappingException;
public class JacksonStreamExample {
public static void main(String[] args) {
try {
JsonFactory jfactory = new JsonFactory();
/*** read from file ***/
JsonParser jParser = jfactory.createJsonParser(new File("D://ocjp//Data.json"));
// loop until token equal to "}"
while (jParser.nextToken() != JsonToken.END_OBJECT) {
String fieldname = jParser.getCurrentName();
if ("Name".equals(fieldname)) {
jParser.nextToken();
System.out.println(jParser.getText());
}
if ("age".equals(fieldname)) {
jParser.nextToken();
System.out.println(jParser.getIntValue()); // display 29
}
if ("hobyy".equals(fieldname)) {
jParser.nextToken(); // current token is "[", move next
// messages is array, loop until token equal to "]"
while (jParser.nextToken() != JsonToken.END_ARRAY) {
// display Reading,Surfing
System.out.println(jParser.getText());
}
}
}
jParser.close();
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
thanks
for any query ping me on pathak.nisarg@yahoo.com
Hello friends.hope you are fine .today i am going to demonstrate how to process json data.there are various API's available in market ,here i am going to demonstrate you Jackson Streaming API Code.
it is API used for processing json data.how ever it process the data/token in incremental manner. it treats individual token as a element.it is faster & convinient way of processing json data.
below sample code creates json file using Jackson Streaming API:
package org.demo;
import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
public class JacksonReader
{
public static void main(String []args) throws IOException
{
JsonFactory jfactory =new JsonFactory();
JsonGenerator jgenerator=jfactory.createJsonGenerator(new File("D:\\ocjp\\Data.json"),JsonEncoding.UTF8);//used to write data in streaming way
jgenerator.writeStartObject();//{
jgenerator.writeStringField("Name","Akshita");//"Name":Akshita
jgenerator.writeNumberField("Age",21);//"Age":21
jgenerator.writeFieldName("hobby");//"messages"jgenerator.writeStartArray(); //[
jgenerator.writeString("Reading");//"Reading"
jgenerator.writeString("Coding");//"Coding"
jgenerator.writeString("Surfing");://"Surfing"
jgenerator.writeEndArray(); //]
jgenerator.close();//close the document
}
}
Sample data.json file that will be generated after compiliation of the above code:
{ "Name":Akshita
"Age":21 ,
"hobby":["Reading","Coding","Surfing"]
};
below code show how to read json data from json file:
import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.JsonMappingException;
public class JacksonStreamExample {
public static void main(String[] args) {
try {
JsonFactory jfactory = new JsonFactory();
/*** read from file ***/
JsonParser jParser = jfactory.createJsonParser(new File("D://ocjp//Data.json"));
// loop until token equal to "}"
while (jParser.nextToken() != JsonToken.END_OBJECT) {
String fieldname = jParser.getCurrentName();
if ("Name".equals(fieldname)) {
jParser.nextToken();
System.out.println(jParser.getText());
}
if ("age".equals(fieldname)) {
jParser.nextToken();
System.out.println(jParser.getIntValue()); // display 29
}
if ("hobyy".equals(fieldname)) {
jParser.nextToken(); // current token is "[", move next
// messages is array, loop until token equal to "]"
while (jParser.nextToken() != JsonToken.END_ARRAY) {
// display Reading,Surfing
System.out.println(jParser.getText());
}
}
}
jParser.close();
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
thanks
for any query ping me on pathak.nisarg@yahoo.com