Jackson Streaming API To Read And Write JSON
2013-12-26 05:10
531 查看
Jackson supports>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, we show you how to use following Jackson streaming APIs to read and write JSON data.
In this example, you use “JsonGenerator”>
As result, following new file named “file.json”>
On the other hand, use
Token>
Token 1 = “{“
Token 2 = “name”
Token 3 = “mkyong”
Token 4 = “}”
See full example.
Warning
The array parsing is a bit tricky, read code comments for explanation.
Output
In summary, for performance critical application, use Steaming API, otherwise, just use normal Jackson data binding.
Reference:http://www.mkyong.com/java/jackson-streaming-api-to-read-and-write-json/
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, we show you how to use following Jackson streaming APIs to read and write JSON data.
JsonGenerator– Write to JSON.
JsonParser– Parse JSON.
1. JsonGenerator
In this example, you use “JsonGenerator”>import java.io.File; import java.io.IOException; import org.codehaus.jackson.JsonEncoding; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.map.JsonMappingException; public class JacksonStreamExample { public static void main(String[] args) { try { JsonFactory jfactory = new JsonFactory(); /*** write to file ***/ JsonGenerator jGenerator = jfactory.createJsonGenerator(new File( "c:\\user.json"), JsonEncoding.UTF8); jGenerator.writeStartObject(); // { jGenerator.writeStringField("name", "mkyong"); // "name" : "mkyong" jGenerator.writeNumberField("age", 29); // "age" : 29 jGenerator.writeFieldName("messages"); // "messages" : jGenerator.writeStartArray(); // [ jGenerator.writeString("msg 1"); // "msg 1" jGenerator.writeString("msg 2"); // "msg 2" jGenerator.writeString("msg 3"); // "msg 3" jGenerator.writeEndArray(); // ] jGenerator.writeEndObject(); // } jGenerator.close(); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
As result, following new file named “file.json”>
{ "name":"mkyong", "age":29, "messages":["msg 1","msg 2","msg 3"] }
2. JsonParser
On the other hand, use JsonParserto>
Token>
{ "name":"mkyong" }
Token 1 = “{“
Token 2 = “name”
Token 3 = “mkyong”
Token 4 = “}”
See full example.
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("c:\\user.json")); // loop until token equal to "}" while (jParser.nextToken() != JsonToken.END_OBJECT) { String fieldname = jParser.getCurrentName(); if ("name".equals(fieldname)) { // current token is "name", // move to next, which is "name"'s value jParser.nextToken(); System.out.println(jParser.getText()); // display mkyong } if ("age".equals(fieldname)) { // current token is "age", // move to next, which is "name"'s value jParser.nextToken(); System.out.println(jParser.getIntValue()); // display 29 } if ("messages".equals(fieldname)) { jParser.nextToken(); // current token is "[", move next // messages is array, loop until token equal to "]" while (jParser.nextToken() != JsonToken.END_ARRAY) { // display msg1, msg2, msg3 System.out.println(jParser.getText()); } } } jParser.close(); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
Warning
The array parsing is a bit tricky, read code comments for explanation.
Output
mkyong 29 msg 1 msg 2 msg 3
Conclusion
In summary, for performance critical application, use Steaming API, otherwise, just use normal Jackson data binding.Reference:http://www.mkyong.com/java/jackson-streaming-api-to-read-and-write-json/
相关文章推荐
- Jackson Streaming API to read and write JSON
- Java Excel API - A Java API to read, write and modify Excel(收集)
- How to use java Properties API -- read and write
- How to read and write JSON files in Java(Gson)
- Read and Write to a Keyboard device in Linux using C++
- Java libraries to read and write PDF files
- JSON.simple example – Read and write JSON Read
- How to mount partition with ntfs file system and read write access
- How To Read and Write XML Documents with GDataXML
- How to Use JSONKit for iOS and the Rotten Tomatoes API
- How To Read and Write XML Documents with GDataXML
- CSpreadSheet - A Class to Read and Write to Excel and Text Delimited Spreadsheet
- Sample: Write And Read data from HDFS with java API
- Use the SQL Server CLR to Read and Write Text Files
- prjGetWriteINIFile - How to Read-Write INI file using VB & API - 读取/写入INI文件 - VB6 + API - SourceCode - HackerJLY
- How to use boost::property_tree to load and write JSON
- Read and Write compressed data to a binary file using ASP.NET
- How to mount partition with ntfs file system and read write access
- Read a Text File with VBA in Excel, and Write the Text to a Spreadsheet
- (转载)XML Tutorial for iOS: How To Read and Write XML Documents with GDataXML