我想保存对象到Sharedpreference
并比想要检索该数据。我将数据存储到hashset
,并使用Gson
将对象转换为json
。其实m存储位图到。我能够将Hashsetobject
转换并保存到sharedpreference
。当我检索并将json
转换为对象时,我遇到了问题。预计BEGIN_ARRAY,但是在BEGIN_OBJECT在第1行第2列,jsonSyntax错误
HashSet<images> img = new HashSet<images>(CIRCLES_LIMIT);
这里是法中Sharedpreference
保存Object
。
public void saveString() throws JSONException {
Object spSquare = c.getStringDrawObjImages();
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = sharedPrefs.edit();
Gson gson = new Gson();
String jsonSquare = gson.toJson(spSquare)
editor.putString("kEySquare", jsonSquare);
editor.commit();
}
检索该对象的方法。
public void openString() {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
Gson gson = new Gson();
String jsonSquare=sharedPrefs.getString("kEySquare",null);
Type typeSquare = new TypeToken<HashSet<images>>(){}.getType();
HashSet<images> arrayListSquare = gson.fromJson(jsonSquare,typeSquare);`//getting Exception here jsonSyntax Error`
if (arrayListSquare != null) {
img = arrayListSquare;
}
}
分享您的JSON – Nikhil
{ \t “IMG”:“[圈[ 218.69626,475.58936,0,android.graphics.Bitmap @ 42e13c70,0.0,0.0,0.0,0.0,0.0,0.0,],Circle [186.74065,670.43713,0,android.graphics.Bitmap @ 42e13c70,0.0,0.0,0.0, 0.0,0.0,0.0,]]“ } –