日期:2014-05-16  浏览次数:20364 次

Jackson学习四之使用Streaming API创建以及读取JSON

参考文章:http://www.mkyong.com/java/jackson-streaming-api-to-read-and-write-json/

?

例子中某些方法已经废弃,直接替换掉了,将原文两个例子放在一个类中:

package com.jingshou.jackson;

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.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

public class JacksonTest6 {

	public static void main(String[] args) throws IOException {
		JsonFactory jfactory = new JsonFactory();
		 
		/*** write to file ***/
		JsonGenerator jGenerator = jfactory.createGenerator(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();
		
		/*** read from file ***/
		JsonParser jParser = jfactory.createParser(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();

	}

}

?

小结:

  • 对于性能要求高的程序,推荐使用流API,否则使用其他方法
  • 不管是创建JsonGenerator还是JsonParser,都是使用JsonFactory

本文出自"lijingshou"博客,转载请务必保留此出处http://lijingshou.iteye.com/blog/2003580