Java 将 ArrayList 转换为字符串并返回到 ArrayList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12276205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java convert ArrayList to string and back to ArrayList?
提问by lisovaccaro
I wanted to save an ArrayList to SharedPreferences so I need to turn it into a string and back, this is what I am doing:
我想将一个 ArrayList 保存到 SharedPreferences 所以我需要把它变成一个字符串然后再回来,这就是我在做什么:
// Save to shared preferences
SharedPreferences sharedPref = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = this.getPreferences(Activity.MODE_PRIVATE).edit();
editor.putString("myAppsArr", myAppsArr.toString());
editor.commit();
I can retrieve it with String arrayString = sharedPref.getString("yourKey", null);
but I don't know how to convert arrayString back into an ArrayList. How can it be done?
我可以检索它,String arrayString = sharedPref.getString("yourKey", null);
但我不知道如何将 arrayString 转换回 ArrayList。怎么做到呢?
My array looks something like:
我的数组看起来像:
[item1,item2,item3]
回答by Dhruv Gairola
You have 2 choices :
您有 2 个选择:
- Manually parse the string and recreate the arraylist. This would be pretty tedious.
Use a JSON library like Google's Gsonlibrary to store and retrieve objects as JSON strings. This is a lightweight library, well regarded and popular. It would be an ideal solution in your case with minimal work required. e.g.,
// 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);
- 手动解析字符串并重新创建数组列表。这会很乏味。
使用像 Google 的Gson库这样的 JSON 库来将对象存储和检索为 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);
回答by Mykhailo Gaidai
Try this
试试这个
ArrayList<String> array = Arrays.asList(arrayString.split(","))
This will work if comma is used as separator and none of the items have it.
如果逗号用作分隔符并且没有任何项目具有它,这将起作用。
回答by bhoomika
//arraylist convert into String using Gson
Gson gson = new Gson();
String data = gson.toJson(myArrayList);
Log.e(TAG, "json:" + gson);
//String to ArrayList
Gson gson = new Gson();
arrayList=gson.fromJson(data, new TypeToken<List<Friends>>()
{}.getType());
回答by lisovaccaro
I ended up using:
我最终使用了:
ArrayList<String> appList = new ArrayList<String>(Arrays.asList(appsString.split("\s*,\s*")));
This doesn't work for all array types though. This option differs from:
但这并不适用于所有数组类型。此选项不同于:
ArrayList<String> array = Arrays.asList(arrayString.split(","));
on that the second option creates an inmutable array.
第二个选项创建一个不可变数组。
回答by user5752479
The page http://mjiayou.com/2015/07/22/exception-gson-internal-cannot-be-cast-to/contains the following:
该页面http://mjiayou.com/2015/07/22/exception-gson-internal-cannot-be-cast-to/包含以下内容:
Type type = new TypeToken<List<T>>(){}.getType();
List<T> list = gson.fromJson(jsonString, type)
perhaps it will be helpful.
也许它会有所帮助。
回答by Quick learner
Update to Dhruv Gairola's answer for Kotlin
更新 Dhruv Gairola 对Kotlin的回答
val gson = Gson();
val jsonString = gson.toJson(arrayList)