Reputation: 21
How to store JSON object which contain list in shared preference Json object like:
{
"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"
},
How to get data when stored in shared perfernces
Upvotes: 2
Views: 2609
Reputation: 719
Convert the JsonArray to the String by calling the following code
JsonArray json = // object
save it to the SharedPreferences
try {
SharedPreferences.Editor editor = mPrefs.edit();
String string new Gson().toJson(json);
editor.putString("jSonData", string);
editor.commit();
} catch (Exception e) {
e.printStackTrace();
}
Retrieve it back!
Gson g = new Gson();
String result = mPrefs.getString("jSonData",null)
g.fromJson(result, JsonArray.class);
Upvotes: 0
Reputation: 2405
Try this
Store
SharedPreferences.Editor editor = getSharedPreferences("MY_PREFS_NAME", MODE_PRIVATE).edit();
editor.putString("jsonKEY","Your values from json parsing" );
editor.commit();
Retrieve it
SharedPreferences prefs = getSharedPreferences("MY_PREFS_NAME", MODE_PRIVATE);
String name = prefs.getString("jsonKEY", null);
Upvotes: 0
Reputation: 417
You can use Gson like this:-
Add dependency:-
compile 'com.google.code.gson:gson:2.2.4'
While saving data:-
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();
}
While getting data:-
String markers1 = mPrefs.getString("jSonData", null);
if (markers1 != null) {
java.lang.reflect.Type type = new TypeToken<ArrayList<String>>() {
}.getType();
newArrayList = gson.fromJson(markers1, type);
}
Upvotes: 0
Reputation: 102
Create POJO like :
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;
}
}
Remember - Do the needful parsing.
Then create following class
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;
}
}
Make sure u are adding this line -
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
Upvotes: 0
Reputation: 13153
1.convert jsonObject
to a String
String yourString = jsonObject.toString();
2.Save it
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("YOURKEY",yourString );
editor.commit();
3.Retrieve it
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.
}
Upvotes: 2
Reputation: 146
At the time of storing data you should do like this way.
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("JsonDataKey", yourJsonData);
editor.commit();
and for getting json string
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String josnData = prefs.getString("JsonDataKey", "nothing");
}
Upvotes: 1
Reputation: 2535
Just convert your json into string and store in key like this:
Editor editor = sharedpreferences.edit();
editor.putString("json_key", "value");
editor.commit();
And then to get back your json do this:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String storedJson= preferences.getString("json_key", null);
Upvotes: 0
Reputation: 1368
String myJson = jsonObject.toString();
editor.putString("MY_JSON", myJson);
editor.commit();
Upvotes: 1