public synchronized <T> T parseJson(String json, Class<T> classOfT) throws Exception{ T target = null; Gson gson = new Gson(); target = gson.fromJson(json, classOfT); return target; }
之后在我的Activity中使用方式是:
String addModel = JsonParse.getJsonParse().parseJson(jsonString, AddModel.class);之后一般编译没问题,但是混淆编译就会报错: shortMsg:java.lang.ClassCastException
//其中AddModel是自己写的一个model类
//jsonString是一个本地文件,以Json格式存在
-keep public class com.android.chebaob.model.AddModel{ *; }
而且要在proguard.cfg文件最后添加:
##---------------Begin: proguard configuration for Gson ---------- # Gson uses generic type information stored in a class file when working with fields. Proguard # removes such information by default, so configure it to keep all of it. -keepattributes Signature # For using GSON @Expose annotation -keepattributes *Annotation* # Gson specific classes -keep class sun.misc.Unsafe { *; } #-keep class com.google.gson.stream.** { *; } # Application classes that will be serialized/deserialized over Gson -keep class com.google.gson.examples.android.model.** { *; } ##---------------End: proguard configuration for Gson ----------
这样就能解决问题了