【问题标题】:how to store each json array in different SharedPref Android如何将每个json数组存储在不同的SharedPref Android中
【发布时间】: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");
                        }

我继续进入异常。 谢谢

【问题讨论】:

标签: android json parsing sharedpreferences storage


【解决方案1】:

如果您可以提供所需的输出,我们将很容易为您提供帮助。但是,您最终会执行以下操作。

SharedPreferences sharedpreferences = getSharedPreferences(PREF_KEY, Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();

editor.putString(KEY1, first_json_array_as_string);
editor.putString(KEY1, second_json_array_as_string);
.....
editor.commit(); 

您也可以遍历您的类别数组。分享您所需的输出格式以获得更好的帮助。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-16
相关资源
最近更新 更多