它是确定保存一个JSON数组中的共享preferences? [英] Is it ok to save a JSON array in SharedPreferences?
问题描述
我有一个JSON数组,我需要保存。我在想序列化,但会是更好的只是将其保存为一个字符串的共享preferences,然后重建它,当我需要阅读它?
I have a JSON Array that I need to save. I was thinking about serializing it, but would it be better to just save it as a string in SharedPreferences and then rebuild it when I need to read it in?
推荐答案
在Java中的JSON对象未实现serialaizable开箱。我看到别人扩展类,允许这一点,但您的具体情况,我只想建议存储JSON对象作为一个字符串,并使用其toString()函数。我已经成功与此有关。
The JSON object in Java does not implement serialaizable out of the box. I have seen others extend the class to allow that but for your situation I would simply recommend storing the JSON object as a string and using its toString() function. I have had success with this.
editor.putString("jsondata", jobj.toString());
和把它找回来:
String strJson = sharedPref.getString("jsondata","0");//second parameter is necessary ie.,Value to return if this preference does not exist.
if(strJson != null) JSONObject jsonData = new JSONObject(strJson);
<一个href="http://developer.android.com/reference/org/json/JSONObject.html#JSONObject(java.lang.String)">http://developer.android.com/reference/org/json/JSONObject.html#JSONObject(java.lang.String)
这篇关于它是确定保存一个JSON数组中的共享preferences?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!