我想转换我的ArrayList<object>
到 JSON String
并返回ArrayList<object>
但我真的不知道该怎么做:
类似于:
Convert
ArrayList<data> arrayData = new ArrayList<data>();
JSONObject retObject = new JSONObject();
retObject.put("data", new JSONArray(arrayData ));
Return convert
idk...
最佳答案
您可以考虑使用 JSON 库(例如 Google 的 Gson 库)以 JSON 字符串的形式存储和检索对象。这是一个轻量级的库,备受推崇且受欢迎。对于您的情况来说,这将是一个理想的解决方案,只需最少的工作。例如,
// How to store JSON string
Gson gson = new Gson();
// This can be any object. Does not have to be an arraylist.
String json = gson.toJson(myAppsArr);
// How to retrieve your Java object back from the string
Gson gson = new Gson();
DataObject obj = gson.fromJson(arrayString, ArrayList.class);
关于java - 如何将 ArrayList<object> 转换为 String,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24056275/