She*_*lam 30 java serialization android json sharedpreferences
我有一个需要保存的JSON数组.我正在考虑将其序列化,但是将它作为字符串保存在SharedPreferences中然后在我需要读取它时重建它会更好吗?
sga*_*man 65
Java中的JSON对象不能实现可开箱即用的可串行化.我已经看到其他人扩展了类以允许它,但是对于你的情况,我只是建议将JSON对象存储为字符串并使用其toString()函数.我成功了.
editor.putString("jsondata", jobj.toString());
Run Code Online (Sandbox Code Playgroud)
并把它拿回来:
String strJson = sharedPref.getString("jsondata","0");//second parameter is necessary ie.,Value to return if this preference does not exist.
if (strJson != null) {
try {
JSONObject response = new JSONObject(strJson);
} catch (JSONException e) {
}
}
Run Code Online (Sandbox Code Playgroud)
http://developer.android.com/reference/org/json/JSONObject.html#JSONObject(java.lang.String)
| 归档时间: |
|
| 查看次数: |
35559 次 |
| 最近记录: |