# GSON – 将 JSON 数组解析为 Java 数组或列表

> 原文： [https://howtodoinjava.com/gson/gson-parse-json-array/](https://howtodoinjava.com/gson/gson-parse-json-array/)

了解如何使用 Google GSON 库将包含 *json 数组*的 [JSON](https://restfulapi.net/introduction-to-json/) 反序列化或解析为 Java [数组](https://howtodoinjava.com/java-array/)或 Java 列表对象。

值得一提的是，JSON 仅具有[数组数据类型](https://restfulapi.net/json-array/)。 Java 同时具有 – [数组](https://howtodoinjava.com/java-array/)和[列表](https://howtodoinjava.com/java-arraylist/)。

## 1.将 JSON 数组解析为根对象

```java
[
    {
      "name": "Alex",
      "id": 1
    },
    {
      "name": "Brian",
      "id": 2
    },
    {
      "name": "Charles",
      "id": 3
    }
]

```

```java
public class User 
{
	private long id;
	private String name;

	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + "]";
	}
}

```

#### 1.1 对象数组

Java 程序将 json 数组**作为根反序列化** – 为 Java 对象数组。

```java
String userJson = "[{'name': 'Alex','id': 1}, "
				+ "{'name': 'Brian','id':2}, "
				+ "{'name': 'Charles','id': 3}]";

Gson gson = new Gson(); 

User[] userArray = gson.fromJson(userJson, User[].class);  

for(User user : userArray) {
	System.out.println(user);
}

```

程序输出。

```java
User [id=1, name=Alex]
User [id=2, name=Brian]
User [id=3, name=Charles]

```

#### 1.2 对象列表

Java 程序将 json 数组作为根反序列化 – 为 Java 对象列表。

```java
import java.lang.reflect.Type;
import java.util.ArrayList;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

String userJson = "[{'name': 'Alex','id': 1}, "
				+ "{'name': 'Brian','id':2}, "
				+ "{'name': 'Charles','id': 3}]";

Gson gson = new Gson(); 

Type userListType = new TypeToken<ArrayList<User>>(){}.getType();

ArrayList<User> userArray = gson.fromJson(userJson, userListType);  

for(User user : userArray) {
	System.out.println(user);
}

```

程序输出：

```java
User [id=1, name=Alex]
User [id=2, name=Brian]
User [id=3, name=Charles]

```

## 2.解析 JSON 数组作为成员

如果它们是非根对象，则 Gson 可以轻松地将 JSON 数组解析为成员。 我们可以按常规方式使用`fromJson()`方法，它将正确地将 json 数组解析为所需的 Java 数组或列表。

```java
{
	"id" 	: 1,
	"name"	: "HR",
	"users" : [
	    {
	      "name": "Alex",
	      "id": 1
	    },
	    {
	      "name": "Brian",
	      "id": 2
	    },
	    {
	      "name": "Charles",
	      "id": 3
	    }
	]

```

#### 2.1 成员数组

Java 程序将**反序列化 json 数组作为成员对象** – 转换为 Java 对象数组作为成员字段。

```java
public class Department 
{
	private long id;
	private String name;
	private User[] users;

	//Getters and Setters

	@Override
	public String toString() {
		return "Department [id=" + id + ", name=" + name + ", users=" + Arrays.toString(users) + "]";
	}
}

```

```java
String departmentJson = "{'id' : 1, "
		+ "'name': 'HR',"
		+ "'users' : ["
			+ "{'name': 'Alex','id': 1}, "
			+ "{'name': 'Brian','id':2}, "
			+ "{'name': 'Charles','id': 3}]}";

Gson gson = new Gson(); 

Department department = gson.fromJson(departmentJson, Department.class);  

System.out.println(department);

```

程序输出：

```java
Department [id=1, name=HR, 
	users=[User [id=1, name=Alex], 
		User [id=2, name=Brian], 
		User [id=3, name=Charles]]]

```

#### 2.2 成员列表

Java 程序**将 json 数组反序列化为成员对象** – 到 Java 对象列表的成员字段。

```java
public class Department 
{
	private long id;
	private String name;
	private List<User> users;

	//Getters and Setters

	@Override
	public String toString() {
		return "Department [id=" + id + ", name=" + name + ", users=" + users + "]";
	}
}

```

```java
String departmentJson = "{'id' : 1, "
		+ "'name': 'HR',"
		+ "'users' : ["
			+ "{'name': 'Alex','id': 1}, "
			+ "{'name': 'Brian','id':2}, "
			+ "{'name': 'Charles','id': 3}]}";

Gson gson = new Gson(); 

Department department = gson.fromJson(departmentJson, Department.class);  

System.out.println(department);

```

程序输出：

```java
Department [id=1, name=HR, 
	users=[User [id=1, name=Alex], 
		User [id=2, name=Brian], 
		User [id=3, name=Charles]]]

```

向我提供有关 **gson 将 JSON 数组解析为 Java 列表和数组**的问题。

学习愉快！

参考文献：

[GSON Github](https://github.com/google/gson)