Jackson中JsonGenerator使用示例

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import org.junit.Test;

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

public class JsonGeneratorDemo {
    @Test
    public void test() throws IOException {
        JsonFactory factory = new JsonFactory();

        JsonGenerator generator = factory.createGenerator(
                new File("src/data/output.json"), JsonEncoding.UTF8);

        generator.writeStartObject();
        generator.writeStringField("brand", "Mercedes");
        generator.writeNumberField("doors", 6);
        generator.writeEndObject();

        generator.close();
    }
}
posted @ 2021-10-08 15:51  xl4ng  阅读(752)  评论(0编辑  收藏  举报