我想保存對象到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,]]「 } –