我收到此JSON字符串:Retrofit - 如何用不同类型的元素分析数组?
{
"response": [
346,
{
"id": 564,
"from_id": -34454802,
"to_id": -34454802,
"date": 1337658196,
"post_type": "post"
},
{
"id": 2183,
"from_id": -34454802,
"to_id": -34454802,
"date": 1423916628,
"post_type": "post"
},
{
"id": 2181,
"from_id": -34454802,
"to_id": -34454802,
"date": 1423724270,
"post_type": "post"
}]
}
创建以下类:
public class Response {
@SerializedName("response")
ArrayList<Post> posts;
}
public class Post {
int id;
int from_id;
int to_id;
long date;
String post_type;
}
当我尝试解析响应,我得到错误:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER at line 1 column 19 path $.response[0]
这是因为数组的第一个元素是数字。需要哪种模型才能正常运行?
您的'JSON'响应字符串无效。你可以在这里查看http://json.parser.online.fr/ – 2015-02-24 12:36:25
对不起,我编辑帖子 – 2015-02-24 12:46:02
你如何期待346号码被翻译成“Post”类型的对象? – splinter123 2015-02-24 13:05:36