更新时间:2022-04-21 21:49:44
以下代码应该适用于您的情况.
Below code should work for your case.
List<Customer> customerList = CustomerDB.selectAll();
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(customerList, new TypeToken<List<Customer>>() {}.getType());
if (! element.isJsonArray() ) {
// fail appropriately
throw new SomeException();
}
JsonArray jsonArray = element.getAsJsonArray();
哎呀,使用 List
接口在将其转换为 JSON 树之前收集值.
Heck, use List
interface to collect values before converting it JSON Tree.