【问题标题】:Android Json Shared preferenceAndroid Json 共享偏好
【发布时间】:2017-05-17 11:30:27
【问题描述】:

如何在共享首选项中存储包含列表的 JSON 对象 Json 对象如:

{
      "id": 6,
      "name": "1-B",
      "section": "B",
      "batchCode": "1-2015-2016-B",
      "courseId": 1,
      "sessionId": 2,
      "course": null,
      "startDate": "2015-03-31",
      "endDate": "2016-03-30"
    },
    {
      "id": 5,
      "name": "1-A",
      "section": "A",
      "batchCode": "1-2015-2016-A",
      "courseId": 1,
      "sessionId": 2,
      "course": null,
      "startDate": "2015-03-31",
      "endDate": "2016-03-30"
    },

存储在共享性能中时如何获取数据

【问题讨论】:

标签: android json android-sharedpreferences


【解决方案1】:

通过调用以下代码将JsonArray转换为String

JsonArray json = // object

将其保存到 SharedPreferences

try {
        SharedPreferences.Editor editor = mPrefs.edit();
        String string new Gson().toJson(json);
        editor.putString("jSonData", string);
        editor.commit();
} catch (Exception e) {
        e.printStackTrace();

}

把它找回来!

Gson g = new Gson();
String result = mPrefs.getString("jSonData",null)
g.fromJson(result, JsonArray.class);

【讨论】:

    【解决方案2】:
     Try this
    

    商店

     SharedPreferences.Editor editor = getSharedPreferences("MY_PREFS_NAME",    MODE_PRIVATE).edit();
    editor.putString("jsonKEY","Your values from json parsing"  );
    editor.commit();
    

    找回来

     SharedPreferences prefs = getSharedPreferences("MY_PREFS_NAME", MODE_PRIVATE); 
    String name = prefs.getString("jsonKEY", null);
    

    【讨论】:

      【解决方案3】:

      你可以像这样使用 Gson:-

      添加依赖:-

      compile 'com.google.code.gson:gson:2.2.4'
      

      保存数据时:-

      Gson gson;
      gson = new Gson();
      
      try {
              SharedPreferences.Editor editor = mPrefs.edit();
              String json = gson.toJson(yourArrayList);
              editor.putString("jSonData", json);
              editor.commit();
          } catch (Exception e) {
              e.printStackTrace();
          }
      

      获取数据时:-

      String markers1 = mPrefs.getString("jSonData", null);
              if (markers1 != null) {
                  java.lang.reflect.Type type = new TypeToken<ArrayList<String>>() {
                  }.getType();
                  newArrayList = gson.fromJson(markers1, type);
              }
      

      【讨论】:

        【解决方案4】:

        像这样创建 POJO:

        public class JsonObj {
        String id = "";
        String name = "";
        String section = "";
        String batchCode = "";
        String courseId = "";
        String sessionId = "";
        String course = "";
        String startDate = "";
        String endDate = "";
        
        public String getId() {
            return id;
        }
        
        public void setId(String id) {
            this.id = id;
        }
        
        public String getName() {
            return name;
        }
        
        public void setName(String name) {
            this.name = name;
        }
        
        public String getSection() {
            return section;
        }
        
        public void setSection(String section) {
            this.section = section;
        }
        
        public String getCourseId() {
            return courseId;
        }
        
        public void setCourseId(String courseId) {
            this.courseId = courseId;
        }
        
        public String getBatchCode() {
            return batchCode;
        }
        
        public void setBatchCode(String batchCode) {
            this.batchCode = batchCode;
        }
        
        public String getSessionId() {
            return sessionId;
        }
        
        public void setSessionId(String sessionId) {
            this.sessionId = sessionId;
        }
        
        public String getCourse() {
            return course;
        }
        
        public void setCourse(String course) {
            this.course = course;
        }
        
        public String getStartDate() {
            return startDate;
        }
        
        public void setStartDate(String startDate) {
            this.startDate = startDate;
        }
        
        public String getEndDate() {
            return endDate;
        }
        
        public void setEndDate(String endDate) {
            this.endDate = endDate;
        }
        

        }

        记住 - 进行必要的解析。

        然后创建下面的类

        public class SettingPreferences {
        public static void setJsonObjValueInPref(Context context,
                                                 String prefKey, JsonObj jsonObj) {
            Editor editor = context.getSharedPreferences(PREFS_NAME,
                    Context.MODE_PRIVATE).edit();
            Gson gson = new Gson();
            String json = gson.toJson(jsonObj);
            editor.putString(prefKey, json);
            editor.commit();
        
        }
        
        public static JsonObj getJsonObjValueFromPref(
                Context context, String prefKey, String defValue) {
        
            SharedPreferences preferences = context.getSharedPreferences(
                    PREFS_NAME, Context.MODE_PRIVATE);
            Gson gson = new Gson();
            String jsonData = preferences.getString(prefKey, "");
            Type type = new TypeToken<ObjProfileDetails>() {
            }.getType();
            JsonObj jsonObj = new JsonObj();
            jsonObj = gson.fromJson(jsonData, type);
            return jsonObj;
        
        }
        

        }

        确保你添加了这一行 -

        dependencies {
        
        compile 'com.google.code.gson:gson:2.2.4'
        

        }

        【讨论】:

          【解决方案5】:

          1.转换 jsonObjectString

          String yourString = jsonObject.toString();

          2.保存

          SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
           editor.putString("YOURKEY",yourString  );
           editor.commit();
          

          3.检索

            SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
              String name = prefs.getString("YOURKEY", null);//If there is no YOURKEY found null will be the default value.
            }
          

          【讨论】:

            【解决方案6】:

            在存储数据时,您应该这样做。

            SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
            editor.putString("JsonDataKey", yourJsonData);
            editor.commit();
            

            用于获取 json 字符串

            SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
            String restoredText = prefs.getString("text", null);
            if (restoredText != null) {
              String josnData = prefs.getString("JsonDataKey", "nothing");
            }
            

            【讨论】:

              【解决方案7】:

              只需将您的 json 转换为字符串并存储在密钥中,如下所示:

              Editor editor = sharedpreferences.edit();
              editor.putString("json_key", "value");
              editor.commit();
              

              然后要取回您的 json,请执行以下操作:

                  SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
                 String storedJson= preferences.getString("json_key", null);
              

              【讨论】:

                【解决方案8】:
                • 将 jsonobject 转换为字符串

                 String myJson = jsonObject.toString();
                • 现在存储这是共享首选项

                editor.putString("MY_JSON", myJson);
                    editor.commit();

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2019-06-20
                  • 2012-10-24
                  • 2019-12-04
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多