【问题标题】:Json Object Store SharedPreferences?Json 对象存储 SharedPreferences?
【发布时间】:2015-05-15 05:40:38
【问题描述】:

这里我想在 SharedPreferences 中 Json 对象。我不知道这是不是存储 json 对象的最佳方式。

这里我想存储 Json 对象,并在需要时调用它的 Json 对象并获取详细信息来存储和删除详细信息。

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

评论包含我对 myPhoto/Myvideo 的评论的所有详细信息 提及包含我在 cmets 中提及的所有细节 朋友数据包含我的整个朋友列表关注/关注。 如果任何朋友被删除,朋友更新和朋友删除将包含

我如何在共享偏好中存储此类信息。 这是正确的方法吗?

 {
     "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

}

【问题讨论】:

    标签: android json sharedpreferences


    【解决方案1】:

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

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

    【讨论】:

    • 豆类是什么意思?
    • bean 类意味着 ex。我的评论数组有评论类,其中包含数据成员中用于解析的所有对象值。
    【解决方案2】:

    您也可以使用 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;
    }
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2021-12-22
        • 1970-01-01
        • 1970-01-01
        • 2011-09-30
        • 2014-05-23
        • 1970-01-01
        • 1970-01-01
        • 2015-04-02
        相关资源
        最近更新 更多