Android
[Android] 안드로이드 - Shared Preference 에 JSON 데이터 저장하는 방법
NukeOlaf
2019. 12. 21. 14:07
https://stackoverflow.com/questions/41442396/android-json-shared-preference/41442831
1. jsonObject 를 String 으로 변환한다
String yourString = jsonObject.toString();
2. Shared Preference 에 String 으로 변환한 jsonObject 를 저장한다
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("YOURKEY",yourString );
editor.commit();
3. 저장한 String 을 꺼내온다.
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.
}
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) {
}
}