Convert JsonString to a custom java ArrayList

蓝咒 提交于 2019-12-24 21:52:16

问题


My qestion is, how I can convert an saved Json Sting back to an custom ArrayList.

This is how I save my ArrayList:

public void saveLocList(ArrayList<LocItem> locList) {
    SharedPreferences.Editor e = returnPref.edit();
    Gson gson = new Gson();
    String json = gson.toJson(locList);
    e.putString(LOCLIST, json);
    e.commit();
}

And now, when I restore the ArrayList (Json String) it doesn't work with this code:

public ArrayList<LocItem> getLocList() {
    String json = returnPref.getString(LOCLIST, "");
    Type type = (Type) new TypeToken<ArrayList<LocItem>>() {
    }.getType();
    ArrayList<LocItem> inpList = new Gson().fromJson(json, (java.lang.reflect.Type) type);
    return inpList;
}

This is my log:

12-30 22:19:19.395: E/AndroidRuntime(2254): FATAL EXCEPTION: main
12-30 22:19:19.395: E/AndroidRuntime(2254): java.lang.RuntimeException: Unable to start activity ComponentInfo{mypackagename.MainActivity}: java.lang.ClassCastException: com.google.gson.internal.$Gson$Types$ParameterizedTypeImpl cannot be cast to android.renderscript.Type
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.ActivityThread.access$600(ActivityThread.java:153)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.os.Looper.loop(Looper.java:137)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.ActivityThread.main(ActivityThread.java:5289)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at java.lang.reflect.Method.invokeNative(Native Method)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at java.lang.reflect.Method.invoke(Method.java:525)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at dalvik.system.NativeStart.main(Native Method)
12-30 22:19:19.395: E/AndroidRuntime(2254): Caused by: java.lang.ClassCastException: com.google.gson.internal.$Gson$Types$ParameterizedTypeImpl cannot be cast to android.renderscript.Type
12-30 22:19:19.395: E/AndroidRuntime(2254):     at mypackagename.helper.Preferences.getLocList(Preferences.java:152)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at mypackagename.MainActivity.onCreate(MainActivity.java:137)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.Activity.performCreate(Activity.java:5133)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-30 22:19:19.395: E/AndroidRuntime(2254):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)
12-30 22:19:19.395: E/AndroidRuntime(2254):     ... 11 more

I don't know what to change, maybe there is a problem with the savingformat (but it works);

Thanks


回答1:


The error message is telling you it's a problem with casting.

TypeToken.getType() returns an object of class ParameterizedTypeImpl, which implements java.lang.reflect.Type.

However, because of an import statement that I'm assuming you have for android.renderscript.Type, Java is trying to cast the ParameterizedTypeImpl to an android.renderscript.Type.

Simply fix the import statement or use the fully qualified name (java.lang.reflect.Type) as you do later in the code.



来源:https://stackoverflow.com/questions/20847997/convert-jsonstring-to-a-custom-java-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!