将 JsonString 转换为自定义的 java ArrayList [英] Convert JsonString to a custom java ArrayList

查看:27
本文介绍了将 JsonString 转换为自定义的 java ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,如何将保存的 Json Sting 转换回自定义 ArrayList.

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

这就是我保存 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();
}

现在,当我恢复 ArrayList(Json 字符串)时,它不适用于此代码:

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;
}

这是我的日志:

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);

谢谢

推荐答案

错误消息告诉您这是转换有问题.

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

TypeToken.getType() 返回一个类 ParameterizedTypeImpl 的对象,它实现了 java.lang.reflect.Type.

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

但是,由于我假设您对 android.renderscript.Type 有一个导入语句,Java 正在尝试将 ParameterizedTypeImpl 转换为 android.renderscript.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.

只需修复导入语句或使用完全限定名称 (java.lang.reflect.Type),就像您稍后在代码中所做的那样.

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

这篇关于将 JsonString 转换为自定义的 java ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆