前言
值得一提的是JSON只有数组数据类型。Java同时具有 – 数组和列表。
学习使用Google GSON库将包含json数组的JSON反序列化或解析为java 数组或java列表对象。
1.将JSON数组解析为根对象
$title(user.json)
[
{
"name": "Alex",
"id": 1
},
{
"name": "Brian",
"id": 2
},
{
"name": "Charles",
"id": 3
}
]
$title(user.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。对象数组
用于将json数组反序列化为root的 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);
}
输出:
User [id=1, name=Alex]
User [id=2, name=Brian]
User [id=3, name=Charles]
1.2。对象列表
用于将json数组反序列化为root的 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);
}
输出:
User [id=1, name=Alex]
User [id=2, name=Brian]
User [id=3, name=Charles]
2.将JSON数组解析为成员
如果JSON数组是非根对象,它们会毫无困难地将JSON数组解析为成员。我们可以fromJson()
以通常的方式使用该方法,它将正确地将json数组解析为所需的java数组或列表。
$title(department.json)
{
"id" : 1,
"name" : "HR",
"users" : [
{
"name": "Alex",
"id": 1
},
{
"name": "Brian",
"id": 2
},
{
"name": "Charles",
"id": 3
}
]
2.1。成员数组
将json数组反序列化为成员对象的 Java程序- 作为成员字段的Java对象数组。
$title(Department.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) + "]";
}
}
demo
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);
输出:
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对象列为成员字段。
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 + "]";
}
}
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);
输出:
Department [id=1, name=HR,
users=[User [id=1, name=Alex],
User [id=2, name=Brian],
User [id=3, name=Charles]]]
原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/243651.html