我想使用Gson
库,但出现此错误:
java.util.ArrayList无法转换为UserHelper
UserHelper
是我的班级助手:
String user_id="",pass="",show_other_profile="",status="",instagram_link="",
facebook_link="",google_plus="",telegram_link="",website_link="",birthday="";
str
变量的值:[{"user_id":"27","pass":"c3992a14e2887f551119b8b72b08fe12","show_other_profile":"","status":"","instagram_link":"","facebook_link":"","google_plus":"","telegram_link":"","website_link":"","birthday":""}]
UserHelper h = new UserHelper();
Gson gson = new Gson();
Type listType = new TypeToken<List<UserHelper>>() {}.getType();
h = gson.fromJson(res, listType); // error line
解决了
arr_users = gson.fromJson(res, listType);
h = arr_users.get(0);
最佳答案
它表明str
包含UserHelper
数组,并且您试图将其反序列化为ArrayList<UserHelper>
,而该类型无法转换为UserHelper
如果确定列表始终包含一个项目,则可以将最后一行替换为:
h = gson.fromJson(res, listType).get(0);