java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to
在做android解析服务器传来的json时遇到的错误。
服务器传来的数据格式
[{"title":"qq","aipPath":"/jeesite/userfiles/bda5e6b04dee47d6ba64386190f4d434/accept/2016/09/1474182626803_nbspd.aip","delFlag":"0"," id":"7ef6815938394fce88a5873312b661e4","isNewRecord":false}]
之前对应的代码如下:
public static <T> List<T> getObjectList(String jsonString,Class<T> cls){ List<T> list = new ArrayList<T>(); try { Gson gson = new Gson(); list = gson.fromJson(jsonString, new TypeToken<List<T>>(){}.getType()); } catch (Exception e) { e.printStackTrace(); } return list; }
以上解析方法是错误的,修改如下代码:
public static <T> List<T> getObjectList(String jsonString,Class<T> cls){ List<T> list = new ArrayList<T>(); try { Gson gson = new Gson(); JsonArray arry = new JsonParser().parse(jsonString).getAsJsonArray(); for (JsonElement jsonElement : arry) { list.add(gson.fromJson(jsonElement, cls)); } } catch (Exception e) { e.printStackTrace(); } return list; }