Jackson-数据流API

1 什么是数据流API

数据流API(即,Streaming API)读取和写入JSON内容作为离散事件。JsonParser类用于读取数据,而JsonGenerator类用于写入数据。这是这三种方法中功能最强大的一种,开销最低,读/写操作最快。它类似于XML的Stax解析器。

在本文中,将展示如何使用Jackson数据流API读取和写入JSON数据。Streaming API使用令牌的概念,并且要谨慎处理Json的每个细节。以下是我们将在示例中使用的两个类:

2 使用JsonGenerator写入JSON

2.1 编写核心类

MainApp:

package com.yiidian.jackson;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class MainApp {

    public static void main(String args[]){
        try {
            JsonFactory jsonFactory = new JsonFactory();
            JsonGenerator jsonGenerator = jsonFactory.createGenerator(new File("student.json"), JsonEncoding.UTF8);

            jsonGenerator.writeStartObject();

            // "name" : "eric"
            jsonGenerator.writeStringField("name", "eric");

            // "age" : 21
            jsonGenerator.writeNumberField("age", 21);

            // "verified" : false
            jsonGenerator.writeBooleanField("verified", false);

            // "marks" : [100, 90, 85]
            jsonGenerator.writeFieldName("marks");

            // [
            jsonGenerator.writeStartArray();
            // 100, 90, 85
            jsonGenerator.writeNumber(100);
            jsonGenerator.writeNumber(90);
            jsonGenerator.writeNumber(85);
            // ]

            jsonGenerator.writeEndArray();

            jsonGenerator.writeEndObject();
            jsonGenerator.close();

            //student.json文件的结果为
            //{
            //   "name":"eric",
            //   "age":21,
            //   "verified":false,
            //   "marks":[100,90,85]
            //}

            ObjectMapper mapper = new ObjectMapper();
            Map<String,Object> dataMap = mapper.readValue(new File("student.json"), Map.class);

            System.out.println(dataMap.get("name"));
            System.out.println(dataMap.get("age"));
            System.out.println(dataMap.get("verified"));
            System.out.println(dataMap.get("marks"));
        }
        catch (JsonParseException e) { e.printStackTrace(); }
        catch (JsonMappingException e) { e.printStackTrace(); }
        catch (IOException e) { e.printStackTrace(); }
    }

}

2.2 运行测试

student.json文件内容如下:

{"name":"eric","age":21,"verified":false,"marks":[100,90,85]}

3 使用JsonParser读取JSON

3.1 编写核心类

MainApp:

package com.yiidian.jackson;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.JsonMappingException;

import java.io.File;
import java.io.IOException;

/**
 * 一点教程网 - http://www.yiidian.com
 */
public class MainApp {

    public static void main(String args[]){
        MainApp tester = new MainApp();
        try {
            JsonFactory jsonFactory = new JsonFactory();

            JsonGenerator jsonGenerator = jsonFactory.createGenerator(new File(
                    "student.json"), JsonEncoding.UTF8);
            jsonGenerator.writeStartObject();
            jsonGenerator.writeStringField("name", "eric");
            jsonGenerator.writeNumberField("age", 21);
            jsonGenerator.writeBooleanField("verified", false);
            jsonGenerator.writeFieldName("marks");
            jsonGenerator.writeStartArray(); // [
            jsonGenerator.writeNumber(100);
            jsonGenerator.writeNumber(90);
            jsonGenerator.writeNumber(85);
            jsonGenerator.writeEndArray();
            jsonGenerator.writeEndObject();
            jsonGenerator.close();

            //student.json文件结果为
            //{
            //   "name":"eric",
            //   "age":21,
            //   "verified":false,
            //   "marks":[100,90,85]
            //}

            JsonParser jsonParser = jsonFactory.createParser(new File("student.json"));
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                //get the current token
                String fieldname = jsonParser.getCurrentName();
                if ("name".equals(fieldname)) {
                    //move to next token
                    jsonParser.nextToken();
                    System.out.println(jsonParser.getText());
                }
                if("age".equals(fieldname)){
                    //move to next token
                    jsonParser.nextToken();
                    System.out.println(jsonParser.getNumberValue());
                }
                if("verified".equals(fieldname)){
                    //move to next token
                    jsonParser.nextToken();
                    System.out.println(jsonParser.getBooleanValue());
                }
                if("marks".equals(fieldname)){
                    //move to [
                    jsonParser.nextToken();
                    // loop till token equal to "]"
                    while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                        System.out.println(jsonParser.getNumberValue());
                    }
                }
            }
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

3.2 运行测试

热门文章

优秀文章