[英]How to convert ArrayList of Point stored in file as string to Arraylist object using Gson?
I have Arraylist of Points object stored in file as a string.我将 Points 对象的 Arraylist 作为字符串存储在文件中。 I converted it into string using gson library.
我使用 gson 库将其转换为字符串。
for (final VisionDetRet ret : results) {
ArrayList<Point> landmarks = ret.getFaceLandmarks();
Gson gson = new Gson();
FileWriter writer = new FileWriter(Environment.getExternalStorageDirectory()+"fileName.txt", true);
gson.toJson(landmarks, writer);
writer.close();
}
This store list of points in file like这个存储文件中的点列表,如
[{"X":90, "Y":120},{"X":50, "Y":121},...][{"X":90, "Y":120},
{"X":50, "Y":121},...][{"X":90, "Y":120},{"X":50, "Y":121},...]
I want to read string from that file and want to convert back into ArrayList.我想从该文件中读取字符串并想转换回 ArrayList。
You just need to create a TypeToken for the ArrayList
with the specific generic type defined.您只需要使用定义的特定泛型类型为
ArrayList
创建一个 TypeToken。
FileReader reader = new FileReader(Environment.getExternalStorageDirectory()+"fileName.txt");
Type listType = new TypeToken<ArrayList<Point>>() { }.getType();
ArrayList<Point> landmarks = new Gson().fromJson(reader, listType);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.