Hi guys,
In this post i show you how to create JSON file using Jackson Streaming API.
Jackson supports read and write JSON via high-performance Jackson Streaming APIs, or incremental mode. Read this Jackson Streaming APIs document for detail explanation on the benefit of using streaming API.
Jackson’s streaming processing is high-performance, fast and convenient, but it’s also difficult to use, because you need to handle each and every detail of JSON data.
In this tutorial, i show you how to use following Jackson streaming APIs to read and write JSON data.
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
public class WriteJson {
public static void main(String[] args) {
try {
JsonFactory jfactory = new JsonFactory();
/*** Write JSON TO FILE ***/
JsonGenerator jGenerator = jfactory.createGenerator(new File("./src/data.json"), JsonEncoding.UTF8);
jGenerator.writeStartObject(); //{
jGenerator.writeStringField("blogname", "oraclejavatechzone.com");
jGenerator.writeStringField("subject", "java");
jGenerator.writeNumberField("year", 2013);
jGenerator.writeFieldName("posts"); //"posts":
jGenerator.writeStartArray(); //[
jGenerator.writeString("post 1");
jGenerator.writeString("post 2");
jGenerator.writeString("post 3");
jGenerator.writeEndArray(); \\]
jGenerator.writeEndObject(); //}
jGenerator.close();
System.out.println("File write successfully");
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
As result, following new file named “data.json” is created :
In this post i show you how to create JSON file using Jackson Streaming API.
Jackson supports read and write JSON via high-performance Jackson Streaming APIs, or incremental mode. Read this Jackson Streaming APIs document for detail explanation on the benefit of using streaming API.
Jackson’s streaming processing is high-performance, fast and convenient, but it’s also difficult to use, because you need to handle each and every detail of JSON data.
In this tutorial, i show you how to use following Jackson streaming APIs to read and write JSON data.
JsonGenerator
– Write to JSON.JsonParser
– Parse JSON.
JsonGenerator
In this example, you use “JsonGenerator” to write JSON “field name”, “values” and “array of values” into a file name “file.json“. See code comments for self-explanatory.import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
public class WriteJson {
public static void main(String[] args) {
try {
JsonFactory jfactory = new JsonFactory();
/*** Write JSON TO FILE ***/
JsonGenerator jGenerator = jfactory.createGenerator(new File("./src/data.json"), JsonEncoding.UTF8);
jGenerator.writeStartObject(); //{
jGenerator.writeStringField("blogname", "oraclejavatechzone.com");
jGenerator.writeStringField("subject", "java");
jGenerator.writeNumberField("year", 2013);
jGenerator.writeFieldName("posts"); //"posts":
jGenerator.writeStartArray(); //[
jGenerator.writeString("post 1");
jGenerator.writeString("post 2");
jGenerator.writeString("post 3");
jGenerator.writeEndArray(); \\]
jGenerator.writeEndObject(); //}
jGenerator.close();
System.out.println("File write successfully");
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
No comments:
Post a Comment