浮云间
您可以创建一个根Pojo并将这两个列表绑定在一个类中。JSON此外,它比为每个实例生成并稍后连接它要快。请参阅下面的示例,该示例显示了该想法:import com.google.gson.Gson;import com.google.gson.GsonBuilder;import org.apache.commons.lang3.RandomStringUtils;import java.util.Arrays;import java.util.List;import java.util.concurrent.ThreadLocalRandom;public class GsonApp { public static void main(String[] args) { List<Class1> class1s = Arrays.asList(new Class1(), new Class1(), new Class1()); List<Class2> class2s = Arrays.asList(new Class2(), new Class2(), new Class2()); Pojo root = new Pojo(); root.setClass1s(class1s); root.setClass2s(class2s); Gson gson = new GsonBuilder() .setPrettyPrinting() .create(); String json = gson.toJson(root); System.out.println(json); Pojo deserialised = gson.fromJson(json, Pojo.class); System.out.println(deserialised); }}class Pojo { private List<Class1> class1s; private List<Class2> class2s; // getters, setters, toString}class Class1 { private int id = ThreadLocalRandom.current().nextInt(100); // getters, setters, toString}class Class2 { private int age = ThreadLocalRandom.current().nextInt(70); private String name = RandomStringUtils.randomAlphanumeric(5); // getters, setters, toString}上面的代码打印:{ "class1s": [ { "id": 2 }, { "id": 1 }, { "id": 3 } ], "class2s": [ { "age": 12, "name": "qXcaT" }, { "age": 33, "name": "3wXv4" }, { "age": 18, "name": "55XZ6" } ]}波霍斯:Pojo{class1s=[Class1{id=2}, Class1{id=1}, Class1{id=3}], class2s=[Class2{age=12, name='qXcaT'}, Class2{age=33, name='3wXv4'}, Class2{age=18, name='55XZ6'}]}