Jackson Streaming API To Read And Write JSON

本文介绍如何使用 Jackson 的 Streaming API 进行 JSON 数据的读写操作。通过具体示例展示了 JsonGenerator 如何写出 JSON 数据,以及 JsonParser 如何解析 JSON 数据。此外还介绍了在处理 JSON 数组时的一些技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

  1. JsonGenerator – Write to JSON.
  2. 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 JsonParser to> Token>

{
   "name":"mkyong"
}
  1. Token 1 = “{“
  2. Token 2 = “name”
  3. Token 3 = “mkyong”
  4. 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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值