Bmob采用异步加载保护数据的安全,但是在实现一些需要本地操作数据库内容时,就变得很麻烦,幸好有SharedPreferences本地存储可以解决这一问题,但是SharedPreferences一般只能存储布尔、字符串、数值型,并不能存储对象,Json可以解决这一问题但Json太麻烦,封装Json的Gson就很简单。
List<对象>转字符串
public static void setSaveAccount(List<Topic> result) {
Gson gson=new Gson();
String s=gson.toJson(result);
putString(SAVE_ACCOUNT,s);
}
字符串转List<对象>
public static List<Topic> getSaveAccount() {
String result=getString(SAVE_TOPIC);
Gson gson=new Gson();
List<Topic> list=gson.fromJson(result,new TypeToken<List<Topic>>(){}.getType());
return list;
其中putString、getString是封装SharedPreferences的方法,与GSON互转List对象与String无关
更多用法
本文介绍如何使用Gson库将List<对象>转换为字符串并存储到SharedPreferences中,以及如何从SharedPreferences中读取字符串并将其转换回List<对象>。通过这种方式,可以在Android应用中实现数据的本地存储。

1万+

被折叠的 条评论
为什么被折叠?



