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'
}