在这篇快速文章中,我们将使用GSON来序列化和反序列化集合。在这个例子中,我们将Integer
和Employee
对象的集合序列化为JSON表示,并使用TypeToken
将整数的集合反序列化为任意的Java对象。
要在Maven2/3中使用Gson,你可以通过添加以下依赖关系来使用Maven Central中的Gson版本。
<dependencies>
<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>
</dependencies>
###将雇员集合序列化
我们先来创建一个Employee
POJO类。
class Employee {
private String firstName;
private String lastName;
public Employee(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
}
现在,让我们写一段代码来序列化一个雇员对象的集合。
Gson gson = new Gson();
// Serialization of collection of employees
Collection<Employee> employees = Arrays.asList(new Employee("firstName1", "lastName1"),
new Employee("firstName2", "lastName2"),
new Employee("firstName3", "lastName3"),
new Employee("firstName4", "lastName4"),
new Employee("firstName5", "lastName5"));
String empJson = gson.toJson(employees);
System.out.println(empJson);
// De-serialization of employee json to Collection of employee Java objects
String employeeJson = "[\r\n" +
" {\r\n" +
" \"firstName\": \"firstName1\",\r\n" +
" \"lastName\": \"lastName1\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"firstName\": \"firstName2\",\r\n" +
" \"lastName\": \"lastName2\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"firstName\": \"firstName3\",\r\n" +
" \"lastName\": \"lastName3\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"firstName\": \"firstName4\",\r\n" +
" \"lastName\": \"lastName4\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"firstName\": \"firstName5\",\r\n" +
" \"lastName\": \"lastName5\"\r\n" +
" }\r\n" +
"]";
Type type = new TypeToken<Collection<Employee>>() {}.getType();
Collection<Employee> collectionOfEmp = gson.fromJson(employeeJson, type);
System.out.println(collectionOfEmp);
让我们再看一个例子,序列化和反序列化整数的集合。
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Collection<Integer> ints = Arrays.asList(1, 2, 3, 4, 5);
// Serialization of integer
String json = gson.toJson(ints);
System.out.println(json);
// Deserialization of integer
Type collectionType = new TypeToken<Collection<Integer>>() {}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
System.out.println(ints2);
package net.javaguides.gson;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
public class GSONCollectionsExample {
public static void main(String[] args) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Collection < Integer > ints = Arrays.asList(1, 2, 3, 4, 5);
// Serialization of integer
String json = gson.toJson(ints);
System.out.println(json);
// Serialization of collection of employees
Collection < Employee > employees = Arrays.asList(new Employee("firstName1", "lastName1"),
new Employee("firstName2", "lastName2"),
new Employee("firstName3", "lastName3"),
new Employee("firstName4", "lastName4"),
new Employee("firstName5", "lastName5"));
String empJson = gson.toJson(employees);
System.out.println(empJson);
// Deserialization of integer
Type collectionType = new TypeToken < Collection < Integer >> () {}.getType();
Collection < Integer > ints2 = gson.fromJson(json, collectionType);
System.out.println(ints2);
// De-serialization of employee json to Collection of employee Java objects
String employeeJson = "[\r\n" +
" {\r\n" +
" \"firstName\": \"firstName1\",\r\n" +
" \"lastName\": \"lastName1\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"firstName\": \"firstName2\",\r\n" +
" \"lastName\": \"lastName2\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"firstName\": \"firstName3\",\r\n" +
" \"lastName\": \"lastName3\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"firstName\": \"firstName4\",\r\n" +
" \"lastName\": \"lastName4\"\r\n" +
" },\r\n" +
" {\r\n" +
" \"firstName\": \"firstName5\",\r\n" +
" \"lastName\": \"lastName5\"\r\n" +
" }\r\n" +
"]";
Type type = new TypeToken < Collection < Employee >> () {}.getType();
Collection < Employee > collectionOfEmp = gson.fromJson(employeeJson, type);
System.out.println(collectionOfEmp);
}
}
class Employee {
private String firstName;
private String lastName;
public Employee(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
}
输出。
[
1,
2,
3,
4,
5
]
[
{
"firstName": "firstName1",
"lastName": "lastName1"
},
{
"firstName": "firstName2",
"lastName": "lastName2"
},
{
"firstName": "firstName3",
"lastName": "lastName3"
},
{
"firstName": "firstName4",
"lastName": "lastName4"
},
{
"firstName": "firstName5",
"lastName": "lastName5"
}
]
[1, 2, 3, 4, 5]
[net.javaguides.gson.Employee@1698c449, net.javaguides.gson.Employee@5ef04b5, net.javaguides.gson.Employee@5f4da5c3, net.javaguides.gson.Employee@443b7951, net.javaguides.gson.Employee@14514713]
Gson可以序列化一个任意对象的集合,但不能从中反序列化,因为用户没有办法指出结果对象的类型。相反,在反序列化时,集合必须是一个特定的、通用的类型。这是有道理的,在遵循良好的Java编码实践时,很少会出现问题。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/10/gson-serialize-and-deserialize-collections-example.html
内容来源于网络,如有侵权,请联系作者删除!