Jackson写入使用JsonGenerator类,解析使用以TOKEN方式

来源:http://www.mkyong.com/java/jackson-streaming-api-to-read-and-write-json/ 

 

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

流的API. 

1 JACKSON中写一个json文件使用的是JsonGenerator类, 
Java代码   收藏代码
  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import org.codehaus.jackson.JsonEncoding;  
  4. import org.codehaus.jackson.JsonFactory;  
  5. import org.codehaus.jackson.JsonGenerationException;  
  6. import org.codehaus.jackson.JsonGenerator;  
  7. import org.codehaus.jackson.map.JsonMappingException;  
  8.    
  9. public class JacksonStreamExample {  
  10.    public static void main(String[] args) {  
  11.    
  12.      try {  
  13.    
  14.     JsonFactory jfactory = new JsonFactory();  
  15.    
  16.     /*** write to file ***/  
  17.     JsonGenerator jGenerator = jfactory.createJsonGenerator(new File(  
  18.             "c:\\user.json"), JsonEncoding.UTF8);  
  19.     jGenerator.writeStartObject(); // {  
  20.    
  21.     jGenerator.writeStringField("name""mkyong"); // "name" : "mkyong"  
  22.     jGenerator.writeNumberField("age"29); // "age" : 29  
  23.    
  24.     jGenerator.writeFieldName("messages"); // "messages" :  
  25.     jGenerator.writeStartArray(); // [  
  26.    
  27.     jGenerator.writeString("msg 1"); // "msg 1"  
  28.     jGenerator.writeString("msg 2"); // "msg 2"  
  29.     jGenerator.writeString("msg 3"); // "msg 3"  
  30.    
  31.     jGenerator.writeEndArray(); // ]  
  32.    
  33.     jGenerator.writeEndObject(); // }  
  34.    
  35.     jGenerator.close();  
  36.    
  37.      } catch (JsonGenerationException e) {  
  38.    
  39.     e.printStackTrace();  
  40.    
  41.      } catch (JsonMappingException e) {  
  42.    
  43.     e.printStackTrace();  
  44.    
  45.      } catch (IOException e) {  
  46.    
  47.     e.printStackTrace();  
  48.    
  49.      }  
  50.    
  51.    }  
  52.    
  53. }  

输出为: 

"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 = “}” 

代码: 
 
Java代码   收藏代码
  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import org.codehaus.jackson.JsonFactory;  
  4. import org.codehaus.jackson.JsonGenerationException;  
  5. import org.codehaus.jackson.JsonParser;  
  6. import org.codehaus.jackson.JsonToken;  
  7. import org.codehaus.jackson.map.JsonMappingException;  
  8.    
  9. public class JacksonStreamExample {  
  10.    public static void main(String[] args) {  
  11.    
  12.      try {  
  13.    
  14.     JsonFactory jfactory = new JsonFactory();  
  15.    
  16.     /*** read from file ***/  
  17.     JsonParser jParser = jfactory.createJsonParser(new File("c:\\user.json"));  
  18.    
  19.     // loop until token equal to "}"  
  20.     while (jParser.nextToken() != JsonToken.END_OBJECT) {  
  21.    
  22.         String fieldname = jParser.getCurrentName();  
  23.         if ("name".equals(fieldname)) {  
  24.    
  25.             
  26.                   //当前结点为name   
  27.           jParser.nextToken();  
  28.           System.out.println(jParser.getText()); // 输出 mkyong  
  29.    
  30.         }  
  31.    
  32.         if ("age".equals(fieldname)) {  
  33.    
  34.           // 当前结点为age   
  35.                   // move to next, which is "name"'s value  
  36.           jParser.nextToken();  
  37.           System.out.println(jParser.getIntValue()); // display 29  
  38.    
  39.         }  
  40.    
  41.         if ("messages".equals(fieldname)) {  
  42.    
  43.           jParser.nextToken();   
  44.           while (jParser.nextToken() != JsonToken.END_ARRAY) {  
  45.    
  46.                      // display msg1, msg2, msg3  
  47.              System.out.println(jParser.getText());   
  48.    
  49.           }  
  50.    
  51.         }  
  52.    
  53.       }  
  54.       jParser.close();  
  55.    
  56.      } catch (JsonGenerationException e) {  
  57.    
  58.       e.printStackTrace();  
  59.    
  60.      } catch (JsonMappingException e) {  
  61.    
  62.       e.printStackTrace();  
  63.    
  64.      } catch (IOException e) {  
  65.    
  66.       e.printStackTrace();  
  67.    
  68.      }  
  69.    
  70.   }  
  71.    
  72. }  

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

转载于:https://www.cnblogs.com/lee0oo0/articles/2652553.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值