Gson解析报错 java.lang.ClassCastException: com.google.gson.internal.StringMap cannot be cast to XXX

    public static  List getList(String jsonString, Class cls) {
        List list = new ArrayList();
        try {
            Gson gson = new Gson();
            list = gson.fromJson(jsonString, new TypeToken>() {}.getType());
        } catch (Exception e) {
        }
        return list;
    }


改为

    public static  List getList(String jsonString, Class cls) {
        List list = new ArrayList();
        try {
            Gson gson = new Gson();
            list = gson.fromJson(jsonString, new ListOfJson(cls));
        } catch (Exception e) {
        }
        return list;
    }

并增加类ListOfJson

public class ListOfJson implements ParameterizedType {
    private Class wrapped;

    public ListOfJson(Class wrapper) {
        this.wrapped = wrapper;
    }

    @Override
    public Type[] getActualTypeArguments() {
        return new Type[]{wrapped};
    }

    @Override
    public Type getRawType() {
        return List.class;
    }

    @Override
    public Type getOwnerType() {
        return null;
    }
}


你可能感兴趣的:(android笔记)