如何将HashMap <object1,list <object =“” >>保存到sharedPref?

时间:2019-02-20 14:11:44

标签: json hashmap sharedpreferences

I have the following code:
Gson gson = new Gson();
//write
  SharedPreferences.Editor editor = context.getSharedPreferences(ITEM, 0).edit();
        editor.putString(KEY, gson.toJson(hashMapObject));
        editor.apply();

//read
 SharedPreferences prefs = context.getSharedPreferences(ITEM, 0);
        String item = prefs.getString(KEY, null);

        java.lang.reflect.Type type = new TypeToken<HashMap<Object1, List<Object2>>>() {
        }.getType();

        HashMap<Object1, List<Object2>> testHashMap2 = gson.fromJson(item, type);

我收到错误消息:

  

java.lang.IllegalStateException:应为BEGIN_ARRAY,但为STRING   在第1行第3列的路径$。

该如何解决?

0 个答案:

没有答案