How to store json object to shared preferences?

前端 未结 8 1328
栀梦
栀梦 2020-12-21 22:53

I am creating an application,In my app I am getting response and displaing it in custom alert dialog, Till here it works fine, Now what I am trying to do is if user selects

相关标签:
8条回答
  • 2020-12-21 23:44

    The easiest way to store JSON object to shared preference is Just make a JSONObject add values to JSONObject at last save to preference with toString

     private void storeDataToPref() {
        SharedPreferences sharedPreferences = getSharedPreferences("localpref", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", "hello");
            jsonObject.put("number", "aaaa");
            android.util.Log.d("response", jsonObject.toString());
            editor.putString("pref_data", jsonObject.toString()).commit();
        } catch (JSONException json) {
    
        }
    }
    

    For getting result from shared preference first of all read string convert into jsonobject and get your desire value thats solve hope this will help you all thanks

    private void getDataFromPref() {
        SharedPreferences sharedPreferences = getSharedPreferences("localpref", MODE_PRIVATE);
        String value = sharedPreferences.getString("pref_data", "");
        try {
            JSONObject jsonObject = new JSONObject(value);
            String name = jsonObject.getString("name");
            String number = jsonObject.getString("number");
            android.util.Log.d("result", name + "...." + number);
        } catch (Exception e) {
    
        }
    }
    
    0 讨论(0)
  • 2020-12-21 23:45

    SharedPreferences it is prefered for storing small data. Hence to save your entire response it is better you save it to a file (ex: .bin) like so:

     public static synchronized void saveToFileSystem(Context context, Object object, String binFileName) {
            try {
                String tempPath = context.getFilesDir() + "/" + binFileName + ".bin";
                File file = new File(tempPath);
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
                oos.writeObject(object);
                oos.flush();
                oos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    and for reading it you can do smth like:

    public static synchronized Object readFromFileSystem(Context context, String binFileName) {
            Object obj = new Object();
            try {
                String tempPath = context.getFilesDir() + "/" + binFileName + ".bin";
                File file = new File(tempPath);
                if (file.exists()) {
                    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
                    obj = ois.readObject();
                    ois.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return obj;
        }
    
    0 讨论(0)
提交回复
热议问题