假设有以下 JSON 数组:

[
  {
    'name': 'John',
    'age': 30
  },
  {
    'name': 'Jane',
    'age': 25
  }
]

可以使用 Java 中的 'Gson' 库将其转换为对象数组。首先,需要创建一个 Java 类来表示 JSON 对象的结构:

public class Person {
    private String name;
    private int age;
    // getters and setters
}

然后,在 Java 代码中使用以下代码将 JSON 数组转换为对象数组:

Gson gson = new Gson();
String json = "[{"name":"John","age":30},{"name":"Jane","age":25}]";
Person[] people = gson.fromJson(json, Person[].class);

现在,'people' 数组将包含两个 'Person' 对象,分别表示 John 和 Jane:

System.out.println(people[0].getName()); // 输出 'John'
System.out.println(people[1].getAge()); // 输出 25
Java JSON 数组转换为对象数组 - Gson 库指南

原文地址: https://www.cveoy.top/t/topic/mUhu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录