簡體   English   中英

以JSON格式的String形式的全局數據保存在SharedPreferences中嗎?

[英]Global data as String in JSON format saved in SharedPreferences?

在Android中以JSON格式保存全局數據並在需要時通過SharedPreferences訪問它是正確的嗎?

在共享首選項中使用它之前,我將提取它並將其另存為字符串。 應用程序更容易讀取字符串,而不是JSON格式的響應。

因此,請執行以下操作:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();

editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long

editor.commit(); // commit changes

如您在上面所看到的,JSON對象或數組沒有預定義的sharedpreference。 因此,從JSON響應中提取數據到字符串並在SharedPreference中傳遞該字符串比較容易。

希望這可以幫助 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM