Json 对象存储共享首选项?
Json Object Store SharedPreferences?
这里我想 Json 对象在 SharedPreferences 中。我不知道这是不是存储 json 对象的最佳方式。
这里我想存储 Json 对象,每当需要调用它的 Json 对象和 fatch 细节来存储和删除细节。
这是我用来存储数据的json格式。
评论包含我对 myPhoto/Myvideo 的评论的所有细节
提及包含我在评论中提及的所有细节
好友数据包含我的整个好友列表 follow/following。
朋友更新和删除的朋友将包含如果删除任何朋友
如何在共享首选项中存储此类信息。
这样对吗?
{
"data": {
"message": "List of comments",
"comments": [
{
"userId":
"commentId":
"name":
"text":
"dateAdded":
"imageLink":
}
],
"addPosition": "0",
"mentions": [
{
"id":
"name":
},
{
"id":
"name":
],
"videoUserData": {
"userId":
"name":
"description":
"views":
"dateAdded":
"imageLink":
},
"friendsAdded": [
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":,
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
],
"friendsDeleted": [],
"friendsUpdated": [],
"friendsSyncDate": "2015-05-13 17:50:34"
},
"statusCode": 200
}
在我看来,您最好使用 SQLite 数据库,因为您的 JSON 看起来像数据库的结构。
为了更快、更方便地浏览您要存储的数据,当您有大量数据时,SQLite 比 SharedPreferences 更好。
您也可以使用 gson 库来这样做...
gson 还为您提供 JAVA 对象,您还可以将 json 字符串保存到共享首选项 ..
->即时使用..你可以使用这个java对象
->否则使用共享首选项
//serialize method to set json to java object and save shared pref
public static <T> void serialize(Context context, Class<T> tClass, String contents, String key) {
Gson gson = new Gson();
T obj = gson.fromJson(contents, tClass);
SharedPreferences preferencesReader = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferencesReader.edit();
editor.putString(key, gson.toJson(obj));
editor.commit();
}
//deserialize method to get java object
public static <T> T deserialize(Context context, Class<T> tClass, String key) {
T obj = null;
try {
Gson gson = new Gson();
SharedPreferences preferencesReader = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
String value = preferencesReader.getString(key, "0");
obj = gson.fromJson(value, tClass);
} catch (Exception e) {
}
return obj;
}
您可以在“首选项”中将其另存为字符串,但这并不是最好的处理方式。
而是将 json
保存在 private memory
的 .json
文件中或使用 SQLite
。
并从需要的存储位置访问 json 对象。
你基本上可以创建一个 util class 来每次访问它。
这里我想 Json 对象在 SharedPreferences 中。我不知道这是不是存储 json 对象的最佳方式。
这里我想存储 Json 对象,每当需要调用它的 Json 对象和 fatch 细节来存储和删除细节。
这是我用来存储数据的json格式。
评论包含我对 myPhoto/Myvideo 的评论的所有细节 提及包含我在评论中提及的所有细节 好友数据包含我的整个好友列表 follow/following。 朋友更新和删除的朋友将包含如果删除任何朋友
如何在共享首选项中存储此类信息。 这样对吗?
{
"data": {
"message": "List of comments",
"comments": [
{
"userId":
"commentId":
"name":
"text":
"dateAdded":
"imageLink":
}
],
"addPosition": "0",
"mentions": [
{
"id":
"name":
},
{
"id":
"name":
],
"videoUserData": {
"userId":
"name":
"description":
"views":
"dateAdded":
"imageLink":
},
"friendsAdded": [
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":,
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
},
{
"userId":
"name":
"imageLink":
],
"friendsDeleted": [],
"friendsUpdated": [],
"friendsSyncDate": "2015-05-13 17:50:34"
},
"statusCode": 200
}
在我看来,您最好使用 SQLite 数据库,因为您的 JSON 看起来像数据库的结构。
为了更快、更方便地浏览您要存储的数据,当您有大量数据时,SQLite 比 SharedPreferences 更好。
您也可以使用 gson 库来这样做... gson 还为您提供 JAVA 对象,您还可以将 json 字符串保存到共享首选项 ..
->即时使用..你可以使用这个java对象
->否则使用共享首选项
//serialize method to set json to java object and save shared pref
public static <T> void serialize(Context context, Class<T> tClass, String contents, String key) {
Gson gson = new Gson();
T obj = gson.fromJson(contents, tClass);
SharedPreferences preferencesReader = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferencesReader.edit();
editor.putString(key, gson.toJson(obj));
editor.commit();
}
//deserialize method to get java object
public static <T> T deserialize(Context context, Class<T> tClass, String key) {
T obj = null;
try {
Gson gson = new Gson();
SharedPreferences preferencesReader = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
String value = preferencesReader.getString(key, "0");
obj = gson.fromJson(value, tClass);
} catch (Exception e) {
}
return obj;
}
您可以在“首选项”中将其另存为字符串,但这并不是最好的处理方式。
而是将 json
保存在 private memory
的 .json
文件中或使用 SQLite
。
并从需要的存储位置访问 json 对象。
你基本上可以创建一个 util class 来每次访问它。