jackson中的写和解析JSON的api例子

本次介绍的是在jackson中,如何去写和解析json,其中用到的都是jackson自己的
流的API.

1 JACKSON中写一个json文件使用的是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();
 
     }
 
   }
 
}

输出为:
{
"name":"mkyong",
"age":29,
"messages":["msg 1","msg 2","msg 3"]
}
2 解析JSON
  我们把上面输出的JSON文件重新读取出来,再解析, 注意在json中,解析是以TOKEN方式进行的,比如:
  {
   "name":"mkyong"
}
将为解析为:
  Token 1 = “{“
Token 2 = “name”
Token 3 = “mkyong”
Token 4 = “}”

代码:
 
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)) {
 
		  
                  //当前结点为name 
		  jParser.nextToken();
		  System.out.println(jParser.getText()); // 输出 mkyong
 
		}
 
		if ("age".equals(fieldname)) {
 
		  // 当前结点为age 
                  // move to next, which is "name"'s value
		  jParser.nextToken();
		  System.out.println(jParser.getIntValue()); // display 29
 
		}
 
		if ("messages".equals(fieldname)) {
 
		  jParser.nextToken(); 
		  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();
 
     }
 
  }
 
}

最后输出:
mkyong
29
msg 1
msg 2
msg 3

你可能感兴趣的:(json,Jackson)