如何在Java使用 JsonGenerator 生成 JSON?
JavaScript 对象表示法 (JSON) 是一种标准的基于文本的格式,用于表示基于 JavaScript 对象语法的结构化数据。它比 XML 轻量、灵活且速度更快,这也是它被广泛用于服务器和客户端之间的数据交换的原因。如果您曾经在Java企业应用程序上工作,那么您会遇到可能需要为应用程序生成和解析 JSON 数据的任务。例如,RESTful Web 服务广泛使用 JSON 作为请求和响应中数据的格式。
以下示例显示具有名称-值对的 JSON 对象:
{
"firstName": "Duke",
"lastName": "Java",
"age": 18,
"streetAddress": "100 Internet Dr",
"city": "JavaTown",
"state": "JA",
"postalCode": "12345",
"phoneNumbers": [
{ "Mobile": "111-111-1111" },
{ "Home": "222-222-2222" }
]
}
Java provides an API to parse, transform, and query JSON data using either the object model or the streaming model.
对象模型通过创建表示内存中 JSON 数据的树来工作。对象模型通过一次导航整个树来生成 JSON 输出,因此允许需要一次访问树的全部内容的处理。可以导航、分析或修改树。这种方法被认为是灵活的,但它比流模型慢,并且需要更多内存。
Java API provides the javax.json package, it contains a reader interface, a writer interface, and a model builder interface for the object model. The package also contains other utility classes and Java types for JSON elements.
在对象模型中,创建了一个 JSON 对象引用,它表示树的根,可用于导航树或将其作为 JSON 数据写入流。创建的这个 JSON 对象引用可以是 JsonObject 或 JsonArray 类型,它们都是 JsonStructure 的子类型。哪一个取决于文件的格式/内容。
让我们准确而清楚地描述它们中的每一个。
让我们来看看JsonObject 的一些特性
- 写成键值对。
- 键必须是字符串,值必须是有效的 JSON 类型。
- JsonObject 以花括号 { } 开始和结束。
- 顺序并不重要。
JSON 对象的示例代码如下图所示
{
"name" : "GeeksforGeeks",
"description" : "An educational website"
}
让我们来看看 JsonArray 的一些特性
- 用于组织相关项目的集合。
- 值可以是字符串、数字、对象、数组、布尔值或 null 类型。
- JsonArray 以方括号 [ ] 开头和结尾。
- 顺序很重要。
插图:
[1, 2, 3, 4]
JsonObject 和 JsonArray 也可以以嵌套方式使用来表示某种特殊类型的数据。您可以使用数组内的对象或对象内的数组。
{
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
],
"positions" : [
{"department" : "Testing", "role" : "Junior"},
{"department" : "Testing", "role" : "Senior"},
{"department" : "Design", "role" : "Junior"}
]
}
现在我们已经了解了基础知识,我们可以学习如何编码。
现在让我们开始安装 javax.json API。因此,如果您使用的是 Maven 项目,则将此依赖项添加到您的pom.xml文件中:
javax.json
javax.json-api
1.1.4
否则,如果您正在创建一个普通项目,则将javax.json jar 添加到您的库中,否则请从此处下载 jar 文件并按照以下步骤操作
步骤 1:从给定的 JSON 数据创建对象模型
如果我们假设我们已经有一些 JSON 格式的数据,因此想要将其转换为Java JsonObject,那么我们可以使用javax.json.JsonStructure 。
假设我们有一个存储 JSON 数据的sample.txt ,如下所示:
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber":
[
{
"type":"home",
"number":"212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
除了创建数据的对象之外,我们还需要有一种方法来读取该数据!
第 2 步:导航它。
为了导航数据,我们将创建一个函数navigateTree() ,它将我们创建的 JSON 对象/模型作为输入,并且对于它遇到的每个嵌套在 JSON 模型中的对象或数组,它将再次调用导航函数,如果元素是一个值,那么它将打印标准输出。
现在我们可以继续从这个sample.txt文件中创建一个 JsonStructure 的代码,如下所示
例子
Java
// Java Program to create a JsonStructure of an already
// existing data in the JSON format
// Importing required classes
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonString;
import javax.json.JsonStructure;
import javax.json.JsonValue;
// Main class
public class CreateObjectModelFromJSONData {
// Main driver method
public static void main(String args[])
throws FileNotFoundException
{
// JsonReader: an interface that reads a JSON object
// or an array structure from an input source.
JsonReader reader = Json.createReader(
new FileReader("sample.txt"));
// JsonStructure: an interface that is a super type
// for the two structured types in JSON
// (objects and arrays)
JsonStructure jsonst = reader.read();
// navigateTree method takes two arguments: a JSON
// element and a key. The key is used only to help
// print the key-value pairs inside objects.
// Elements in a tree are represented by the
// JsonValue type.
navigateTree(jsonst, null);
}
// A JsonValue is one of the following: an object
// (JsonObject), an array (JsonArray), a number
// (JsonNumber), a string (JsonString), true
// (JsonValue.TRUE), false (JsonValue.FALSE), or null
// (JsonValue.NULL).
// Method 2
// To navigate through the model
// and print the key-value pairs
public static void navigateTree(JsonValue tree,
String key)
{
if (key != null)
System.out.print("Key " + key + ": ");
// Switch case
// Method 3
// getValueType() returns the value type of
// this JSON value.
switch (tree.getValueType()) {
// Case 1
case OBJECT:
System.out.println("OBJECT");
JsonObject object = (JsonObject)tree;
for (String name : object.keySet())
navigateTree(object.get(name), name);
break;
// Case 2
case ARRAY:
System.out.println("ARRAY");
JsonArray array = (JsonArray)tree;
for (JsonValue val : array)
navigateTree(val, null);
break;
// Case 3
case STRING:
JsonString st = (JsonString)tree;
System.out.println("STRING " + st.getString());
break;
// Case 4
case NUMBER:
JsonNumber num = (JsonNumber)tree;
System.out.println("NUMBER " + num.toString());
break;
// Case 5
case TRUE:
// Case 6
case FALSE:
// Case 7
case NULL:
// Print statement
System.out.println(
tree.getValueType().toString());
break;
}
}
}
Java
// Java Program to create a Json object from scratch using
// JsonObjectBuilder and navigate it.
// Importing required classes
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonString;
import javax.json.JsonValue;
// Main class
// CreateObjectModelFromCode
public class GFG {
// Method 1
// Main driver method
public static void main(String args[])
{
// add() method adds a name/value pair to the JSON
// object associated with this object builder.
// build() method returns the JSON object associated
// with this object builder.
JsonObject model
= Json.createObjectBuilder()
.add("firstName", "Duke")
.add("lastName", "Java")
.add("age", 18)
.add("streetAddress", "100 Internet Dr")
.add("city", "JavaTown")
.add("state", "JA")
.add("postalCode", "12345")
.add("phoneNumbers",
Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("type", "mobile")
.add("number",
"111-111-1111"))
.add(Json.createObjectBuilder()
.add("type", "home")
.add("number",
"222-222-2222")))
.build();
// The same navigateTree() method can be used to
// navigate this model.
navigateTree(model, null);
}
// Method 2
public static void navigateTree(JsonValue tree,
String key)
{
if (key != null)
System.out.print("Key " + key + ": ");
// Method 3- getValueType()
// To get the value types
// Switch case
switch (tree.getValueType()) {
// Case 1
case OBJECT:
System.out.println("OBJECT");
JsonObject object = (JsonObject)tree;
for (String name : object.keySet())
navigateTree(object.get(name), name);
break;
// Case 2
case ARRAY:
System.out.println("ARRAY");
JsonArray array = (JsonArray)tree;
for (JsonValue val : array)
navigateTree(val, null);
break;
// Case 3
case STRING:
JsonString st = (JsonString)tree;
System.out.println("STRING " + st.getString());
break;
// Case 4
case NUMBER:
JsonNumber num = (JsonNumber)tree;
System.out.println("NUMBER " + num.toString());
break;
// Case 5
case TRUE:
// Case 6
case FALSE:
// Case 7
case NULL:
System.out.println(
tree.getValueType().toString());
break;
}
}
}
Java
// Java Progra to Write the Object Model to an Output Stream
// Importing required classes
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.StringWriter;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonStructure;
import javax.json.JsonWriter;
// Main class
// WritingObjectModelToAStream
public class GFG {
// Main driver method
public static void main(String args[])
throws FileNotFoundException
{
JsonReader reader = Json.createReader(
new FileReader("sample.txt"));
JsonStructure jsonst = reader.read();
StringWriter stWriter = new StringWriter();
// We use try-with-resources to close the JSON
// writer automatically
// Json.createWriter() method takes an output stream
// as a parameter. JsonWriter.writeObject() method
// writes the object to the stream.
// Try block to check for exceptions
try (JsonWriter jsonWriter
= Json.createWriter(stWriter)) {
jsonWriter.writeObject((JsonObject)jsonst);
}
// Creating variable jsonData to store the object
// written to the stream in the form of a String
String jsonData = stWriter.toString();
// Print the data string
System.out.println(jsonData);
}
}
Java
// Java program to Read JSON data using a JsonParser
// Importing required classes
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.StringReader;
import java.io.StringWriter;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonStructure;
import javax.json.JsonWriter;
import javax.json.stream.JsonParser;
// Main class
// ReadingJSONDataUsingAParser
public class GFG {
// Main driver method
public static void main(String args[])
throws FileNotFoundException
{
// Creating object of JsonReader class
// Creating object of JsonStructure class
JsonReader reader = Json.createReader(
new FileReader("sample.txt"));
JsonStructure jsonst = reader.read();
StringWriter stWriter = new StringWriter();
// Try block to check for exceptions
try (JsonWriter jsonWriter
= Json.createWriter(stWriter)) {
jsonWriter.writeObject((JsonObject)jsonst);
}
// Step 1
String jsonData = stWriter.toString();
// Step 2
// Json.createParser(): Creates a JSON parser from a
// character stream.
JsonParser parser
= Json.createParser(new StringReader(jsonData));
// Step 3
// haNext(): Returns true if there are more parsing
// states. This method returns false if the parser
// reaches the end of the JSON text
while (parser.hasNext()) {
// JsonParser.Event: An event from JsonParser.
// next(): Returns the event for the next
// parsing state.
JsonParser.Event event = parser.next();
// Step 4
// Switch case
switch (event) {
// Case 1
// Start of a JSON array.
case START_ARRAY:
// Case 2
// End of a JSON array.
case END_ARRAY:
// Case 3
// Start of a JSON object.
case START_OBJECT:
// Case 4
// End of a JSON object.
case END_OBJECT:
// Case 5
// False value in a JSON array or object.
case VALUE_FALSE:
// Case 6
// Null value in a JSON array or object.
case VALUE_NULL:
// Case 7
// True value in a JSON array or object.
case VALUE_TRUE:
System.out.println(event.toString());
break;
// Case 8
// Name in a name/value pair of a JSON object.
case KEY_NAME:
System.out.print(event.toString() + " "
+ parser.getString()
+ " - ");
break;
// Case 9
// String value in a JSON array or object.
case VALUE_STRING:
// Case 10
// Number value in a JSON array or object.
case VALUE_NUMBER:
System.out.println(event.toString() + " "
+ parser.getString());
break;
}
}
}
}
Java
// Java Program to Generate JSON data and Store it into a
// file.
// Importing required classes
import java.io.FileWriter;
import java.io.IOException;
import javax.json.Json;
import javax.json.stream.JsonGenerator;
// Main class
// WriteJSONDataUsingGenerator
public class GFG {
// Main driver method
public static void main(String args[])
{
FileWriter writer = null;
// Try block to check fr exceptions
try {
writer = new FileWriter("details.txt");
}
// Catch blcok to handle i/o exceptions
catch (IOException e) {
// Print the exceptions along with line number
// using printStackTrace() method
e.printStackTrace();
}
// Json.createGenerator(): Creates a JSON generator
// for writing JSON to a character stream.
JsonGenerator generator
= Json.createGenerator(writer);
// writeStartObject(): Writes the JSON name/start
// object character pair in the current object
// context. write(): Writes a JSON name/string value
// pair in the current object context. writeEnd():
// Writes the end of the current context.
// writeStartArray(): Writes the JSON name/start
// array character pair with in the current object
// context.
generator.writeStartObject()
.write("firstName", "John")
.write("lastName", "Smith")
.write("age", 25)
.writeStartObject("address")
.write("streetAddress", "21 2nd Street")
.write("city", "New York")
.write("state", "NY")
.write("postalCode", "10021")
.writeEnd()
.writeStartArray("phoneNumber")
.writeStartObject()
.write("type", "home")
.write("number", "212 555-1234")
.writeEnd()
.writeStartObject()
.write("type", "fax")
.write("number", "646 555-4567")
.writeEnd()
.writeEnd()
.writeEnd();
// Closes this generator and frees any resources
// associated with it using close() method
generator.close();
}
}
输出
OBJECT
Key firstName: STRING John
Key lastName: STRING Smith
Key age: NUMBER 25
Key address: OBJECT
Key streetAddress: STRING 21 2nd Street
Key city: STRING New York
Key state: STRING NY
Key postalCode: STRING 10021
Key phoneNumber: ARRAY
OBJECT
Key type: STRING home
Key number: STRING 212 555-1234
OBJECT
Key type: STRING fax
Key number: STRING 646 555-4567
2. 从代码创建对象模型
为了从头开始创建我们自己的对象模型,我们将使用 JSON 类,该类提供了创建 JSON 对象构建器的createObjectBuilder()方法。 JsonObjectBuilder接口充当从头开始创建 JsonObject 模型的构建器。此接口初始化一个空的 JSON 对象模型,并提供将名称/值对添加到对象模型并返回结果对象的方法。可以链接此类中的方法以向对象添加多个名称/值对。
例子
Java
// Java Program to create a Json object from scratch using
// JsonObjectBuilder and navigate it.
// Importing required classes
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonString;
import javax.json.JsonValue;
// Main class
// CreateObjectModelFromCode
public class GFG {
// Method 1
// Main driver method
public static void main(String args[])
{
// add() method adds a name/value pair to the JSON
// object associated with this object builder.
// build() method returns the JSON object associated
// with this object builder.
JsonObject model
= Json.createObjectBuilder()
.add("firstName", "Duke")
.add("lastName", "Java")
.add("age", 18)
.add("streetAddress", "100 Internet Dr")
.add("city", "JavaTown")
.add("state", "JA")
.add("postalCode", "12345")
.add("phoneNumbers",
Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("type", "mobile")
.add("number",
"111-111-1111"))
.add(Json.createObjectBuilder()
.add("type", "home")
.add("number",
"222-222-2222")))
.build();
// The same navigateTree() method can be used to
// navigate this model.
navigateTree(model, null);
}
// Method 2
public static void navigateTree(JsonValue tree,
String key)
{
if (key != null)
System.out.print("Key " + key + ": ");
// Method 3- getValueType()
// To get the value types
// Switch case
switch (tree.getValueType()) {
// Case 1
case OBJECT:
System.out.println("OBJECT");
JsonObject object = (JsonObject)tree;
for (String name : object.keySet())
navigateTree(object.get(name), name);
break;
// Case 2
case ARRAY:
System.out.println("ARRAY");
JsonArray array = (JsonArray)tree;
for (JsonValue val : array)
navigateTree(val, null);
break;
// Case 3
case STRING:
JsonString st = (JsonString)tree;
System.out.println("STRING " + st.getString());
break;
// Case 4
case NUMBER:
JsonNumber num = (JsonNumber)tree;
System.out.println("NUMBER " + num.toString());
break;
// Case 5
case TRUE:
// Case 6
case FALSE:
// Case 7
case NULL:
System.out.println(
tree.getValueType().toString());
break;
}
}
}
输出
OBJECT
Key firstName: STRING Duke
Key lastName: STRING Java
Key age: NUMBER 18
Key streetAddress: STRING 100 Internet Dr
Key city: STRING JavaTown
Key state: STRING JA
Key postalCode: STRING 12345
Key phoneNumbers: ARRAY
OBJECT
Key type: STRING mobile
Key number: STRING 111-111-1111
OBJECT
Key type: STRING home
Key number: STRING 222-222-2222
3. 将对象模型写入流
我们在上面的示例中创建的对象模型可以使用 JsonWriter 类写入流。 JsonWriter 将 JSON 对象或数组结构写入输出源。这次我们将把sample.txt的内容转换为 JsonStructure 后写入输出流。
例子
Java
// Java Progra to Write the Object Model to an Output Stream
// Importing required classes
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.StringWriter;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonStructure;
import javax.json.JsonWriter;
// Main class
// WritingObjectModelToAStream
public class GFG {
// Main driver method
public static void main(String args[])
throws FileNotFoundException
{
JsonReader reader = Json.createReader(
new FileReader("sample.txt"));
JsonStructure jsonst = reader.read();
StringWriter stWriter = new StringWriter();
// We use try-with-resources to close the JSON
// writer automatically
// Json.createWriter() method takes an output stream
// as a parameter. JsonWriter.writeObject() method
// writes the object to the stream.
// Try block to check for exceptions
try (JsonWriter jsonWriter
= Json.createWriter(stWriter)) {
jsonWriter.writeObject((JsonObject)jsonst);
}
// Creating variable jsonData to store the object
// written to the stream in the form of a String
String jsonData = stWriter.toString();
// Print the data string
System.out.println(jsonData);
}
}
输出:在控制台上,将在一行中打印以下内容
{
"firstName":"John","lastName":"Smith","age":25,
"address":{"streetAddress":"21 2nd Street","city":"New York","state":"NY","postalCode":"10021"},
"phoneNumber":[{"type":"home","number":"212 555-1234"},{"type":"fax","number":"646 555-4567"}]
}
现在考虑下一个模型,
流模型使用基于事件的解析器,一次读取一个元素的 JSON 数据。流模型通过一次使用一个元素进行函数调用,为给定的流生成 JSON 输出。这种方法适用于本地处理,其中元素的处理不需要来自其余数据的信息。它的工作方式是解析器生成事件并在它找到键、找到值或到达对象或数组的开头或结尾时停止处理。可以根据代码处理或丢弃找到的元素,然后解析器移动到下一个事件。
Java API 提供了javax.json.stream包,它包含一个解析器接口 JsonParser 和一个生成器接口 JsonGenerator 用于流模型。接口 JsonParser 包含以流方式解析 JSON 的方法。接口 JsonGenerator 包含以流方式将 JSON 写入输出源的方法。
In the upcoming code, we will make use of the same sample.txt file to parse it.
1. 使用解析器读取 JSON 数据:我们将使用 JsonParser,它以流式方式提供对 JSON 数据的前向只读访问。 JsonParser 使用拉解析编程模型解析 JSON。在此模型中,客户端代码控制线程并调用方法 next() 在处理每个元素后将解析器推进到下一个状态。解析器可以生成以下事件:
START_OBJECT, END_OBJECT, START_ARRAY, END_ARRAY, KEY_NAME, VALUE_STRING, VALUE_NUMBER, VALUE_TRUE, VALUE_FALSE, and VALUE_NULL.
在下面的代码中,我们将执行以下步骤:
- 以String的形式获取JSON数据,以便传递给解析器对象。
- 通过调用 JSON.createParser 静态方法获取解析器实例并将字符串传递给它。
- 使用 JsonParser.hasNext 和 JsonParser.next 方法迭代解析器事件。
- 对每个元素执行本地处理。
执行:
例子
Java
// Java program to Read JSON data using a JsonParser
// Importing required classes
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.StringReader;
import java.io.StringWriter;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonStructure;
import javax.json.JsonWriter;
import javax.json.stream.JsonParser;
// Main class
// ReadingJSONDataUsingAParser
public class GFG {
// Main driver method
public static void main(String args[])
throws FileNotFoundException
{
// Creating object of JsonReader class
// Creating object of JsonStructure class
JsonReader reader = Json.createReader(
new FileReader("sample.txt"));
JsonStructure jsonst = reader.read();
StringWriter stWriter = new StringWriter();
// Try block to check for exceptions
try (JsonWriter jsonWriter
= Json.createWriter(stWriter)) {
jsonWriter.writeObject((JsonObject)jsonst);
}
// Step 1
String jsonData = stWriter.toString();
// Step 2
// Json.createParser(): Creates a JSON parser from a
// character stream.
JsonParser parser
= Json.createParser(new StringReader(jsonData));
// Step 3
// haNext(): Returns true if there are more parsing
// states. This method returns false if the parser
// reaches the end of the JSON text
while (parser.hasNext()) {
// JsonParser.Event: An event from JsonParser.
// next(): Returns the event for the next
// parsing state.
JsonParser.Event event = parser.next();
// Step 4
// Switch case
switch (event) {
// Case 1
// Start of a JSON array.
case START_ARRAY:
// Case 2
// End of a JSON array.
case END_ARRAY:
// Case 3
// Start of a JSON object.
case START_OBJECT:
// Case 4
// End of a JSON object.
case END_OBJECT:
// Case 5
// False value in a JSON array or object.
case VALUE_FALSE:
// Case 6
// Null value in a JSON array or object.
case VALUE_NULL:
// Case 7
// True value in a JSON array or object.
case VALUE_TRUE:
System.out.println(event.toString());
break;
// Case 8
// Name in a name/value pair of a JSON object.
case KEY_NAME:
System.out.print(event.toString() + " "
+ parser.getString()
+ " - ");
break;
// Case 9
// String value in a JSON array or object.
case VALUE_STRING:
// Case 10
// Number value in a JSON array or object.
case VALUE_NUMBER:
System.out.println(event.toString() + " "
+ parser.getString());
break;
}
}
}
}
输出:
START_OBJECT
KEY_NAME firstName - VALUE_STRING John
KEY_NAME lastName - VALUE_STRING Smith
KEY_NAME age - VALUE_NUMBER 25
KEY_NAME address - START_OBJECT
KEY_NAME streetAddress - VALUE_STRING 21 2nd Street
KEY_NAME city - VALUE_STRING New York
KEY_NAME state - VALUE_STRING NY
KEY_NAME postalCode - VALUE_STRING 10021
END_OBJECT
KEY_NAME phoneNumber - START_ARRAY
START_OBJECT
KEY_NAME type - VALUE_STRING home
KEY_NAME number - VALUE_STRING 212 555-1234
END_OBJECT
START_OBJECT
KEY_NAME type - VALUE_STRING fax
KEY_NAME number - VALUE_STRING 646 555-4567
END_OBJECT
END_ARRAY
END_OBJECT
2. 使用 Generator 编写 JSON 数据
使用 JsonGenerator,我们可以以流式方式将 JSON 数据写入输出源。通过调用 Json.createGenerator 静态方法获取 JSON 生成器,该方法以 writer 或输出流作为参数。我们将 JSON 数据写入details.txt文件。
例子
Java
// Java Program to Generate JSON data and Store it into a
// file.
// Importing required classes
import java.io.FileWriter;
import java.io.IOException;
import javax.json.Json;
import javax.json.stream.JsonGenerator;
// Main class
// WriteJSONDataUsingGenerator
public class GFG {
// Main driver method
public static void main(String args[])
{
FileWriter writer = null;
// Try block to check fr exceptions
try {
writer = new FileWriter("details.txt");
}
// Catch blcok to handle i/o exceptions
catch (IOException e) {
// Print the exceptions along with line number
// using printStackTrace() method
e.printStackTrace();
}
// Json.createGenerator(): Creates a JSON generator
// for writing JSON to a character stream.
JsonGenerator generator
= Json.createGenerator(writer);
// writeStartObject(): Writes the JSON name/start
// object character pair in the current object
// context. write(): Writes a JSON name/string value
// pair in the current object context. writeEnd():
// Writes the end of the current context.
// writeStartArray(): Writes the JSON name/start
// array character pair with in the current object
// context.
generator.writeStartObject()
.write("firstName", "John")
.write("lastName", "Smith")
.write("age", 25)
.writeStartObject("address")
.write("streetAddress", "21 2nd Street")
.write("city", "New York")
.write("state", "NY")
.write("postalCode", "10021")
.writeEnd()
.writeStartArray("phoneNumber")
.writeStartObject()
.write("type", "home")
.write("number", "212 555-1234")
.writeEnd()
.writeStartObject()
.write("type", "fax")
.write("number", "646 555-4567")
.writeEnd()
.writeEnd()
.writeEnd();
// Closes this generator and frees any resources
// associated with it using close() method
generator.close();
}
}
输出:在details.txt文件中,以下数据将写入一行:
{
"firstName":"John","lastName":"Smith","age":25,
"address":{"streetAddress":"21 2nd Street","city":"New York","state":"NY","postalCode":"10021"},
"phoneNumber":[{"type":"home","number":"212 555-1234"},{"type":"fax","number":"646 555-4567"}]
}