Gson 简明教程
Gson - Class
Gson 是 Google Gson 库的主要操作员类。它提供将 Java 对象转换成匹配 JSON 构造的过程,反之亦然。使用 GsonBuilder 首先构建 Gson,然后使用 toJson(Object) 或 fromJson(String, Class) 方法来读/写 JSON 构造。
Gson is the main actor class of Google Gson library. It provides functionalities to convert Java objects to matching JSON constructs and vice versa. Gson is first constructed using GsonBuilder and then, toJson(Object) or fromJson(String, Class) methods are used to read/write JSON constructs.
Class Declaration
以下为 com.google.gson.Gson 类的声明 −
Following is the declaration for com.google.gson.Gson class −
public final class Gson
extends Object
Constructors
Sr.No |
Constructor & Description |
1 |
Gson() Constructs a Gson object with default configuration. |
Class Methods
Sr.No |
Method & Description |
1 |
<T> T fromJson(JsonElement json, Class<T> classOfT) This method deserializes the Json read from the specified parse tree into an object of the specified type. |
2 |
<T> T fromJson(JsonElement json, Type typeOfT) This method deserializes the Json read from the specified parse tree into an object of the specified type. |
3 |
<T> T fromJson(JsonReader reader, Type typeOfT) Reads the next JSON value from reader and convert it to an object of type typeOfT. |
4 |
<T> T fromJson(Reader json, Class<T> classOfT) This method deserializes the Json read from the specified reader into an object of the specified class. |
5 |
<T> T fromJson(Reader json, Type typeOfT) This method deserializes the Json read from the specified reader into an object of the specified type. |
6 |
<T> T fromJson(String json, Class<T> classOfT) This method deserializes the specified Json into an object of the specified class. |
7 |
<T> T fromJson(String json, Type typeOfT) This method deserializes the specified Json into an object of the specified type. |
8 |
<T> TypeAdapter<T> getAdapter(Class<T> type) Returns the type adapter for type. |
9 |
<T> TypeAdapter<T> getAdapter(TypeToken<T> type) Returns the type adapter for type. |
10 |
<T> TypeAdapter<T> getDelegateAdapter(TypeAdapterFactory skipPast, TypeToken<T> type) This method is used to get an alternate type adapter for the specified type. |
11 |
String toJson(JsonElement jsonElement) Converts a tree of JsonElements into its equivalent JSON representation. |
12 |
void toJson(JsonElement jsonElement, Appendable writer) Writes out the equivalent JSON for a tree of JsonElements. |
13 |
void toJson(JsonElement jsonElement, JsonWriter writer) Writes the JSON for jsonElement to writer. |
14 |
String toJson(Object src) This method serializes the specified object into its equivalent Json representation. |
15 |
void toJson(Object src, Appendable writer) This method serializes the specified object into its equivalent Json representation. |
16 |
String toJson(Object src, Type typeOfSrc) This method serializes the specified object, including those of generic types, into its equivalent Json representation. |
17 |
void toJson(Object src, Type typeOfSrc, Appendable writer) This method serializes the specified object, including those of generic types, into its equivalent Json representation. |
18 |
void toJson(Object src, Type typeOfSrc, JsonWriter writer) Writes the JSON representation of src of type typeOfSrc to writer. |
19 |
JsonElement toJsonTree(Object src) This method serializes the specified object into its equivalent representation as a tree of JsonElements. |
20 |
JsonElement toJsonTree(Object src, Type typeOfSrc) This method serializes the specified object, including those of generic types, into its equivalent representation as a tree of JsonElements. |
21 |
String toString() |
Methods inherited
此类从以下类继承方法 −
This class inherits methods from the following class −
-
java.lang.Object
Example
在任何您选择的编辑器中创建以下 Java 程序,并将它保存在,比如,C:/> GSON_WORKSPACE
Create the following Java program using any editor of your choice, and save it at, say, C:/> GSON_WORKSPACE
File − GsonTester.java
File − GsonTester.java
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GsonTester {
public static void main(String[] args) {
String jsonString = "{\"name\":\"Mahesh\", \"age\":21}";
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
Gson gson = builder.create();
Student student = gson.fromJson(jsonString, Student.class);
System.out.println(student);
jsonString = gson.toJson(student);
System.out.println(jsonString);
}
}
class Student {
private String name;
private int age;
public Student(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student [ name: "+name+", age: "+ age+ " ]";
}
}
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
Student [ name: Mahesh, age: 21 ]
{
"name" : "Mahesh",
"age" : 21
}