【发布时间】:2014-03-20 04:28:50
【问题描述】:
我有以下 JSON 提要:
{
collection_name: "My First Collection",
username: "Alias",
collection: {
1: {
photo_id: 1,
owner: "Some Owner",
title: "Lightening McQueen",
url: "http://hesp.suroot.com/elliot/muzei/public/images/randomhash1.jpg"
},
2: {
photo_id: 2,
owner: "Awesome Painter",
title: "Orange Plane",
url: "http://hesp.suroot.com/elliot/muzei/public/images/randomhash2.jpg"
}
}
}
我要做的是获取集合的内容 - photo_id、所有者、标题和 URL。我有以下代码,但是我收到 GSON JSON 错误:
@GET("/elliot/muzei/public/collection/{collection}")
PhotosResponse getPhotos(@Path("collection") String collectionID);
static class PhotosResponse {
List<Photo> collection;
}
static class Photo {
int photo_id;
String title;
String owner;
String url;
}
}
我认为我的代码对于获取 JSON 提要是正确的,但我不太确定。任何帮助表示赞赏。
我得到的错误是:
Caused by: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 75
但是我很难理解如何使用 GSON 库
【问题讨论】:
-
您收到什么样的错误?请问是Logcat吗?
-
基本上,关键行是: 引起:retrofit.converter.ConversionException:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期 BEGIN_ARRAY 但在第 1 行第 75 列是 BEGIN_OBJECT跨度>
-
好的,所以我对 gson 并不是非常熟悉,但似乎它期待的是
Photos的数组,而不是其中的List。 -
@K20GH 这是您正在解析的实际 JSON 吗?我不这么认为,因为
Column 75会在第一个photo_id的中间。
标签: java android json retrofit