Gson 简明教程
Gson - Streaming
Streaming API 用于逐个读取 JSON token。它将 JSON 内容读入和写入为离散事件。 JsonReader 和 JsonWriter 以 token 形式读/写数据,称为 JsonToken 。
Streaming API is used to read JSON token by token. It reads and writes JSON content as discrete events. JsonReader and JsonWriter read/write the data as token, referred as JsonToken.
在三种处理 JSON 的方法中它是最强大的方法。它开销最低而且读/写操作非常快。它类似于 XML 的 Stax 解析器。
It is the most powerful approach among the three approaches to process JSON. It has the lowest overhead and it is quite fast in read/write operations. It is analogous to Stax parser for XML.
在本章中,我们将展示 GSON 流式 API 以读取 JSON 数据。流式 API 使用 token 的概念,Json 的每个细节都必须小心处理。
In this chapter, we will showcase the usage of GSON streaming APIs to read JSON data. Streaming API works with the concept of token and every details of Json is to be handled carefully.
//create JsonReader object and pass it the json source or json text.
JsonReader reader = new JsonReader(new StringReader(jsonString));
//start reading json
reader.beginObject();
//get the next token
JsonToken token = reader.peek();
//check the type of the token
if (token.equals(JsonToken.NAME)) {
//get the current token
fieldname = reader.nextName();
}
Example
让我们看看 JsonReader 如何发挥作用。在 C:\>GSON_WORKSPACE 中创建一个名为 GsonTester 的 Java 类文件。
Let’s see JsonReader in action. Create a Java class file named GsonTester in C:\>GSON_WORKSPACE.
File - GsonTester.java
import java.io.IOException;
import java.io.StringReader;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
public class GsonTester {
public static void main(String args[]) {
String jsonString =
"{\"name\":\"Mahesh Kumar\", \"age\":21,\"verified\":false,\"marks\": [100,90,85]}";
JsonReader reader = new JsonReader(new StringReader(jsonString));
try {
handleJsonObject(reader);
}
catch (IOException e) {
e.printStackTrace();
}
}
private static void handleJsonObject(JsonReader reader) throws IOException {
reader.beginObject();
String fieldname = null;
while (reader.hasNext()) {
JsonToken token = reader.peek();
if (token.equals(JsonToken.BEGIN_ARRAY)) {
System.out.print("Marks [ ");
handleJsonArray(reader);
System.out.print("]");
} else if (token.equals(JsonToken.END_OBJECT)) {
reader.endObject();
return;
} else {
if (token.equals(JsonToken.NAME)) {
//get the current token
fieldname = reader.nextName();
}
if ("name".equals(fieldname)) {
//move to next token
token = reader.peek();
System.out.println("Name: "+reader.nextString());
}
if("age".equals(fieldname)) {
//move to next token
token = reader.peek();
System.out.println("Age:" + reader.nextInt());
}
if("verified".equals(fieldname)) {
//move to next token
token = reader.peek();
System.out.println("Verified:" + reader.nextBoolean());
}
}
}
}
private static void handleJsonArray(JsonReader reader) throws IOException {
reader.beginArray();
String fieldname = null;
while (true) {
JsonToken token = reader.peek();
if (token.equals(JsonToken.END_ARRAY)) {
reader.endArray();
break;
} else if (token.equals(JsonToken.BEGIN_OBJECT)) {
handleJsonObject(reader);
} else if (token.equals(JsonToken.END_OBJECT)) {
reader.endObject();
} else {
System.out.print(reader.nextInt() + " ");
}
}
}
}
Verify the result
使用以下 javac 编译器编译类:
Compile the classes using javac compiler as follows −
C:\GSON_WORKSPACE>javac GsonTester.java
现在运行 GsonTester 以查看结果 −
Now run the GsonTester to see the result −
C:\GSON_WORKSPACE>java GsonTester
验证输出。
Verify the output.
Name: Mahesh Kumar
Age:21
Verified:false
Marks [ 100 90 85 ]