Is It Ok to Save a JSON Array in Sharedpreferences

Is it ok to save a JSON array in SharedPreferences?

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());

And to get it back:

String strJson = sharedPref.getString("jsondata","0");//second parameter is necessary ie.,Value to return if this preference does not exist. 

if (strJson != null) {
try {
JSONObject response = new JSONObject(strJson);

} catch (JSONException e) {

}
}

http://developer.android.com/reference/org/json/JSONObject.html#JSONObject(java.lang.String)

How to store and retrieve JSONArray in Android studio sharedpreferences

You can use GSON.

JSONArray jArr = new JSONArray();
editor.putString("YOURKEY", jArr.toString());
editor.commit();

And this one to read:

JSONArray jArr = (new Gson()).fromJson(preferences.getString("YOURKEY"), JSONArray.class));

As a really good alternative, you can use Hawk library. It's a secure, simple key-value storage for android using shared preferences that supports object storage and encryption.

How to store json object to shared preferences?

if you want to store the response the better way is to write the content into a file.if you want to store only some values you can do like this

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("key_from_json", "String_value_from_json");
editor.putInt("key_from_json","int_value_from_json");
editor.commit();

how to store the json in sharedpreference and retrieve them in another activity one by one?

First of all create a jsonobject using your result string

try {
JSONObject MyObject=new JSONObject("{\"NEW_DISPLAY_SCHEDULE\":[{\"ScheduleDate\":\"2018-03-01\",\"StartTime\":\"9:10 am\",\"Endtime\":\"11:30 am\"},{\"ScheduleDate\":\"2018-03-02\",\"StartTime\":\"2:00 pm\",\"Endtime\":\"4:00 pm\"},{\"ScheduleDate\":\"2018-03-03\",\"StartTime\":\"5:00 pm\",\"Endtime\":\"6:00 pm\"},{\"ScheduleDate\":\"2018-03-04\",\"StartTime\":\"2:00 pm\",\"Endtime\":\"6:00 pm\"},{\"ScheduleDate\":\"2018-03-05\",\"StartTime\":\"RTO\",\"Endtime\":\"\"},{\"ScheduleDate\":\"2018-03-06\",\"StartTime\":\"10:00 am\",\"Endtime\":\"11:00 pm\"},{\"ScheduleDate\":\"2018-03-06\",\"StartTime\":\"2:00 pm\",\"Endtime\":\"4:00 pm\"},{\"ScheduleDate\":\"2018-03-31\",\"StartTime\":\"6:00 am\",\"Endtime\":\"4:00 pm\"},{\"ScheduleDate\":\"2018-03-14\",\"StartTime\":\"7:00 am\",\"Endtime\":\"4:00 pm\"},{\"ScheduleDate\":\"2018-03-28\",\"StartTime\":\"1:00 pm\",\"Endtime\":\"4:00 pm\"}]}");
} catch (JSONException e) {
e.printStackTrace();
}

then do like this

SharedPreferences  preferences = getSharedPreferences("identifier", Context.MODE_PRIVATE);

In your 1st activity do this to save

Editor prefsEditor = preferences.edit();
Gson gson = new Gson();
String json = gson.toJson(MyObject);
prefsEditor.putString("MyResultObject", json);
prefsEditor.commit();

and in 2nd activity retrieve it like this

Gson gson = new Gson();
String json = preferences.getString("MyResultObject", "");
JSONObject obj = gson.fromJson(json, JSONObject.class);

Now to get any key from this json object,

try {
JSONArray jsonArray=obj.getJSONArray("NEW_DISPLAY_SCHEDULE");
for (int i=0;i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
String ScheduleDate=jsonObject.getString("ScheduleDate");
ScheduleDateArray.add(ScheduleDate);
//add toast here to display the value of ScheduleDate in first array element
Toast.makeText(Main2Activity.this , "ScheduleDates"+ ScheduleDate, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}

Note : First initialise this >> private ArrayList<String> ScheduleDateArray=new ArrayList<>();



Related Topics



Leave a reply



Submit