解决步骤:
1:使用Gson gson.toJson 先转为String
2:再转为相应集合数据
代码:
Gson gson = new Gson();
String gsonStr = gson.toJson(response.data);
indexBeans = GsonUtils.gsonToList(gsonStr, IndexListBean.class);
public class GsonUtils {
private static Gson gson = null;
static {
initGson();
}
private GsonUtils() {
}
public static void initGson() {
if (gson == null) {
gson = new Gson();
}
}
public static <T> List<T> gsonToList(String gsonString, Class<T> cls) {
List<T> list = new ArrayList<>();
if (TextUtils.isEmpty(gsonString)) {
return list;
}
initGson();
if (gson != null) {
JsonArray array = new JsonParser().parse(gsonString).getAsJsonArray();
for (final JsonElement elem : array) {
list.add(gson.fromJson(elem, cls));
}
}
return list;
}
}
网友评论