首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Json对象存储SharedPreferences?

Json对象存储SharedPreferences?
EN

Stack Overflow用户
提问于 2015-05-15 05:40:38
回答 3查看 81关注 0票数 0

在这里,我想在SharedPreferences中使用Json对象。我不知道这是存储json对象的最好方法。

在这里,我想存储Json对象,并在需要时调用它的Json对象和fatch细节来存储和删除细节。

这是我用来存储数据的json格式。

评论包含了我在myPhoto/Myvideo提到的评论的所有细节,在评论中包含了我提到的所有细节,“朋友”数据包含了我的整个朋友列表,接下来/下面。朋友更新和删除的朋友将包含如果任何朋友删除

如何将这类信息存储在共享偏好中。这条路对吗?

代码语言:javascript
复制
 {
     "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

}

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-05-15 05:46:26

在我看来,使用SQLite数据库会更好,因为您的JSON看起来像数据库的结构。

为了更快、更方便地浏览您将要存储的数据,当您拥有大量数据时,SQLite比SharedPreferences更好。

票数 0
EN

Stack Overflow用户

发布于 2015-05-15 05:57:44

您也可以使用gson库这样做。gson还为您提供JAVA对象,还可以将json字符串保存为共享首选项。

->for即时使用..you可以使用这个java对象

->otherwise使用共享首选项

代码语言:javascript
复制
  //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;
}
票数 0
EN

Stack Overflow用户

发布于 2015-05-15 06:17:53

您可以在首选项中将其保存为字符串,但这不是最好的处理方法。相反,将json保存在private memory中的.json文件中,或者使用SQLite。并在需要的地方从存储位置访问json对象。

您基本上可以创建一个util类来每次访问它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30252269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档