Java使用 gson 解析出现的错误
错误:ExpectedBEGIN_ARRAYbutwasBEGIN_OBJECTJson数据是:{"results":[{"createdAt":"2016-11-051...
错误:Expected BEGIN_ARRAY but was BEGIN_OBJECT
Json数据是:
{"results":[
{"createdAt":"2016-11-05 13:10:21","matchU1":"K2","objectId":"37T5444G","state":false,"updatedAt":"2016-11-05 13:23:09"},
{"createdAt":"2016-11-05 13:10:36","matchU1":"12","objectId":"1pPvDDDE","state":false,"updatedAt":"2016-11-05 13:10:39"},
{"createdAt":"2016-11-05 13:10:52","matchU1":"1","objectId":"0nSfhhhn","state":false,"updatedAt":"2016-11-05 13:23:02"}]}
JavaBean是:
public class Results {
private String matchU1;
private String matchU2;
private Boolean match1;
private Boolean match2;
private Boolean state;
private String objectId;
get\set方法省略
}
然后gson是:
Gson gson=new Gson();
List<Results> ps = gson.fromJson(str, new TypeToken<List<Results>>(){}.getType());
会报错 应该怎么修改? 展开
Json数据是:
{"results":[
{"createdAt":"2016-11-05 13:10:21","matchU1":"K2","objectId":"37T5444G","state":false,"updatedAt":"2016-11-05 13:23:09"},
{"createdAt":"2016-11-05 13:10:36","matchU1":"12","objectId":"1pPvDDDE","state":false,"updatedAt":"2016-11-05 13:10:39"},
{"createdAt":"2016-11-05 13:10:52","matchU1":"1","objectId":"0nSfhhhn","state":false,"updatedAt":"2016-11-05 13:23:02"}]}
JavaBean是:
public class Results {
private String matchU1;
private String matchU2;
private Boolean match1;
private Boolean match2;
private Boolean state;
private String objectId;
get\set方法省略
}
然后gson是:
Gson gson=new Gson();
List<Results> ps = gson.fromJson(str, new TypeToken<List<Results>>(){}.getType());
会报错 应该怎么修改? 展开
2个回答
展开全部
应该是你的javaBean写错了
更多追问追答
追问
JavaBean要怎么写呢?
追答
json数据解析的时候,写javaBean中的字段,你可以少写一些字段,但是不能多写,多写了就会报错;你当中的
private Boolean match2;
private String matchU2;
这两个字段没有的吧,你去解析当然会错
还有就是你前面为啥要用boolean类型呢?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询