【发布时间】:2014-08-11 10:13:31
【问题描述】:
如何将我的每个 Json 数组存储到 Android 中的不同 SharedPref 中。
即
我需要将每个类别 [category_1] => Array DATA 存储到 SharedPref 中以便以后访问。
如何运行 Json 并将其存储到 SharedPref。谢谢大家的帮助。
Json:
Array
(
[status] => success
[reason] => success
[version] => 1
[total] => Array
(
[category_1] => 3
[category_2] => 3
[category_3] => 3
)
[title] => Array
(
[category_1] => Food
[category_2] => Drink
[category_3] => Dessert
)
[data] => Array
(
[category_1] => Array
(
[0] => Array
(
[content_id] => 123456
[name] => Name1
[title] => This is title one
[desc] => This is title description
[thumb] => http://image.jpg
)
[1] => Array
(...
)
[category_2] => Array
(
[0] => Array
(
[content_id] => 123456
[name] => Name1
[title] => This is title one
[desc] => This is title description
[thumb] => http://image.jpg
)
[1] => Array
(...
)
[category_3] => Array
(
[0] => Array
(
[content_id] => 123456
[name] => Name1
[title] => This is title one
[desc] => This is title description
[thumb] => http://image.jpg
)
[1] => Array
( ...
)
)
)
更新 _________________________
我用过:
SharedPreferences sharedCategory_1Pref = getSharedPreferences(CATEGORY_1, Context.MODE_PRIVATE);
Editor editor = sharedCategory_1Pref.edit();
editor.putString(CATEGORY_1, jArray_data_category_1.toString());
//System.out.println(jArray_data_category_1.toString());
editor.commit();
我可以将 json 数组保存到 SharePref 中。但现在我无法阅读它,使用这个:
try {
JSONArray jsonArray2 = new JSONArray(sharedCategory_1Pref.getString(CATEGORY_1, status));
for (int i = 0; i < jsonArray2.length(); i++) {
Log.d("your JSON Array", jsonArray2.getInt(i)+"");
Log.e("inside try", "inside try");
}
} catch (Exception e) {
e.printStackTrace();
Log.e("inside Exception", "inside Exception");
}
我继续进入异常。 谢谢
【问题讨论】:
-
你能举一个你想要的输出的例子吗?
-
我提出了另一个更详细的问题,感谢您迄今为止的帮助。 [链接] (stackoverflow.com/questions/25260456/…)
标签: android json parsing sharedpreferences storage