我有以下JSON Feed:
{
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库
答案 0 :(得分:5)
您的JSON无效。
GSON正在BEGIN_ARRAY "["
之后等待collection:
,因为您的PhotosResponse
类定义了一个照片List<Photo>
数组,但找到了BEGIN_OBJECT "{"
,它应该是<{1}} / p>
{
"collection_name": "My First Collection",
"username": "Alias",
"collection": [
{
"photo_id": 1,
"owner": "Some Owner",
"title": "Lightening McQueen",
"url": "http://hesp.suroot.com/elliot/muzei/public/images/randomhash1.jpg"
},
{
"photo_id": 2,
"owner": "Awesome Painter",
"title": "Orange Plane",
"url": "http://hesp.suroot.com/elliot/muzei/public/images/randomhash2.jpg"
}
]
}
也许你从带有键的错误的json_encode()
PHP数组中获取JSON,你应该在没有键的情况下从PHP编码JSON,仅使用数组值(PHP Array to JSON Array using json_encode())