1. Introduction
1.导言
Efficient JSON parsing is one of the most important tasks in Java programming when it comes to data manipulation and communication.
当涉及数据操作和通信时,高效的 JSON 解析是 Java 编程中最重要的任务之一。
The Gson library offers a versatile JsonParser class to simplify the conversion process. Moreover, it’s important to note that this class has been deprecated, eliminating the need for instantiation. Instead, we can utilize the provided static methods for the conversion process.
Gson 库提供了一个通用的 JsonParser 类来简化转换过程。此外,值得注意的是,该类已被弃用,因此无需实例化。相反,我们可以利用所提供的静态方法来完成转换过程。
In this tutorial, we’ll delve into how to utilize the static methods instead of the deprecated JsonParser for efficient JSON parsing in Java.
在本教程中,我们将深入探讨如何利用静态方法而不是已废弃的 JsonParser 在 Java 中进行高效的 JSON 解析。
2. Deprecated JsonParser
2.已弃用的 JsonParser
Here is an example of using the deprecated JsonParser to parse a JSON string:
下面是一个使用已废弃的 JsonParser 解析 JSON 字符串的示例:
String jsonString = "{\"name\": \"John\", \"age\":30, \"city\":\"New York\"}";
JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject();
The deprecated JsonParser instance may still function, but developers are encouraged to move on with new and improved practices.
已弃用的 JsonParser 实例仍可正常运行,但我们鼓励开发人员采用新的改进实践。
3. Embracing Static Methods
3.采用静态方法
The Gson library offers static methods as replacements for the deprecated ones. Moreover, it is a more elegant and easier-to-understand parsing way of JSON.
Gson库提供了静态方法来替代已废弃的方法。此外,它还是一种更优雅、更易于理解的 JSON 解析方式。
Let’s explore the recommended static methods:
让我们来探讨一下推荐的静态方法:
3.1. Parse from String
3.1.从 String 解析
We can parse a JSON string directly into JsonObject without using a deprecated instance of JsonParser using the parseString() static method.
我们可以使用 parseString() 静态方法将 JSON 字符串直接解析为 JsonObject 而无需使用已废弃的 JsonParser 实例。
Firstly, let’s set up a JSON string describing person-related data and read an associated JsonObject with given keys like name, age, and city underlying class constructor of DeprecatedJsonParserUnitTest:
首先,让我们设置一个描述个人相关数据的 JSON 字符串,并读取一个关联的 JsonObject ,其给定键为 姓名、年龄和 城市,其底层类构造函数为 DeprecatedJsonParserUnitTest: 。
String jsonString = "{\"name\": \"John\", \"age\":30, \"city\":\"New York\"}";
JsonObject expectedJsonObject = new JsonObject();
DeprecatedJsonParserUnitTest() {
expectedJsonObject.addProperty("name", "John");
expectedJsonObject.addProperty("age", 30);
expectedJsonObject.addProperty("city", "New York");
}
Now, let’s parse the jsonString directly into JsonObject:
现在,让我们将 jsonString 直接解析为 JsonObject:
@Test
public void givenJsonString_whenUsingParseString_thenJsonObjectIsExpected() {
JsonObject jsonObjectAlt = JsonParser.parseString(jsonString).getAsJsonObject();
assertEquals(expectedJsonObject, jsonObjectAlt);
}
In this test method, we verify that the parsed jsonObjectAlt matches the expectedJsonObject created earlier.
在此测试方法中,我们将验证解析后的 jsonObjectAlt 是否与之前创建的 expectedJsonObject 匹配。
3.2. Parse from StringReader
3.2.从 StringReader 进行解析
There are cases when the obtained JSON data comes from a StringReader. We can use the parseReader() static method to get the same result without using obsolete components:
我们可以使用 parseReader() 静态方法来获得相同的结果,而无需使用过时的组件:
@Test
public void givenJsonString_whenUsingParseReader_thenJsonObjectIsExpected() {
StringReader reader = new StringReader(jsonString);
JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject();
assertEquals(expectedJsonObject, jsonObject);
}
Here, we initialize a StringReader called reader. Then, we use the JsonParser.parseReader() method to parse the JSON data into a JsonObject.
在这里,我们初始化一个名为 reader 的 StringReader 。然后,我们使用 JsonParser.parseReader() 方法将 JSON 数据解析为一个 JsonObject 。
3.3. Parse from JsonReader
3.3.从 JsonReader 进行解析
When dealing with a JsonReader, the parseReader() static method is still an effective and contemporary decision that avoids outdated constructions. Let’s take an example:
在处理 JsonReader 时,parseReader() 静态方法仍然是一种有效的现代方法,可以避免过时的结构。让我们举个例子:
@Test
public void givenJsonReader_whenParseUsingJsonReader_thenJsonObjectIsExpected() {
JsonReader jsonReader = new JsonReader(new StringReader(jsonString));
JsonObject jsonObject = JsonParser.parseReader(jsonReader).getAsJsonObject();
assertEquals(expectedJsonObject, jsonObject);
}
In the above test method, we begin by instantiating a JsonReader named jsonReader with the contents of the JSON string. Then, we utilize the JsonParser.parseReader() method to parse such JSON data into a JsonObject.
在上述测试方法中,我们首先使用 JSON 字符串的内容实例化一个名为 jsonReader 的 JsonReader 。然后,我们利用 JsonParser.parseReader() 方法将这些 JSON 数据解析为一个 JsonObject 。
4. Conclusion
4.结论
In conclusion, JsonParser was deprecated, and there are excellent alternative static methods provided by the Gson class, such as parseString(), parseReader(), and parseJson().
总之,JsonParser 已被弃用,Gson 类提供了出色的替代静态方法,例如 parseString()、parseReader() 和 parseJson() 。
As always, the complete code samples for this article can be found over on GitHub.
与往常一样,本文的完整代码示例可在 GitHub 上找到。