Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Json对象存储共享引用?_Android_Json_Sharedpreferences - Fatal编程技术网

Android Json对象存储共享引用?

Android Json对象存储共享引用?,android,json,sharedpreferences,Android,Json,Sharedpreferences,这里我想在SharedReferences中创建Json对象。我不知道这是不是存储json对象的最佳方式 这里我想存储Json对象,当需要调用它的Json对象和fatch细节来存储和删除细节时 这是我存储数据的json格式 评论包含我对myPhoto/Myvideo的所有评论细节 提及包含我在评论中提及的所有细节 好友数据包含我的整个好友列表follow/follow。 好友更新和好友删除将包含是否删除任何好友 如何在共享首选项中存储此类信息。 这条路对吗 { "data": {

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

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

这是我存储数据的json格式

评论包含我对myPhoto/Myvideo的所有评论细节 提及包含我在评论中提及的所有细节 好友数据包含我的整个好友列表follow/follow。 好友更新和好友删除将包含是否删除任何好友

如何在共享首选项中存储此类信息。 这条路对吗

 {
     "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优于SharedReferences。

您也可以使用gson库来实现这一点。。。 gson还为您提供JAVA对象,您还可以将json字符串保存到共享首选项

->供即时使用..您可以使用此java对象

->否则,请使用共享首选项


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


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

bean类的意思是什么?bean类的意思是“我的注释数组”有一个注释类,它包含数据成员中的所有对象值,用于解析。
  //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;
}