Skip to content

Save Objects

Hendra Permana edited this page May 6, 2017 · 3 revisions

You can save your (Gson) Objects as an Array.

Save from object array

.saveObjects(arrayObject)

Example :

Profile[] mProfiles mProfiles = new Profile[jsonArray.length()];
for(int i = 0; i < mProfiles.length; i++){
    mProfiles[i] = new Gson().fromJson(jsonArray.get(i).toString(), Profile.class);
}
gsonSharedPreferences.saveObjects(mProfiles);

Save from JSON Array

.saveObjects(object, jsonArray)

Example, Profile.class is your object :

jsonArray = new JSONArray(jsonArrayString);
gsonSharedPreferences.saveObjects(new Profile(), jsonArray);

Save from JSON Array string

.saveObjects(object, jsonArrayString)

Example Profile.class is your object :

  • jsonArrayString :
[{
		"user_id": 667,
		"user_name": "blog",
		"website": "http://blog.hynra.com"
	},
	{
		"user_id": 668,
		"user_name": "devnote",
		"website": "http://devnote.hynra.com"
	}
]
  • code :
gsonSharedPreferences.saveObjects(new Profile(), jsonArrayString);
Clone this wiki locally