我试图将这个json bellow解析为一个名为AlbumResponse
的对象,其中包含另外两个对象Album
和PaginationInfo
。改造版本2.0
[
{
"id": "6",
"name": "King Stays King",
"artist_name":"Timbaland",
"image":""
},
{
"id": "7",
"name": "East Atlanta Santa 2",
"artist_name":"Gucci Mane",
"image":""
},
{
"id": "8",
"name": "The cuban connect",
"artist_name":"Phophit",
"image":""
},
{
"id": "9",
"name": "Shmoney Keeps",
"artist_name":"Calling",
"image":""
},
{
"id": "10",
"name": "Cabin Fever 3",
"artist_name":"Wiz khalifa",
"image":""
}
],
{
"nextPage": "http://private-ede172-mymixtapez1.apiary-mock.com/features/page_3/",
"itemsTotal": 10,
"page": 2,
"pagerMax": 2
}
专辑类
public class Album {
long id;
String name;
@SerializedName("artist_name")
String artistName;
String image;
}
PaginationInfo类
public class PaginationInfo {
int page;
int pagerMax;
int nextPage;
int itemsTotal;
}
AlbumResponse,上面有两个类,Album
是List
public class AlbumResponse {
public List<Album> albums;
public PaginationInfo paginationInfo;
}
请求
Call<AlbumResponse> responseCall = albumService.features();
responseCall.enqueue(new Callback<AlbumResponse>() {
@Override
public void onResponse(Response<AlbumResponse> response, Retrofit retrofit) {
if(response.isSuccess()) {
AlbumResponse albumResponse = response.body();
PaginationInfo paginationInfo = albumResponse.getPaginationInfo();
}
System.out.println();
}
@Override
public void onFailure(Throwable t) {
System.out.println(t.getMessage());
}
});
接口
public interface AlbumService {
@GET("/features/")
Call<AlbumResponse> features();
}
问题是我得到的Throwable
包含:
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第2列路径为BEGIN_ARRAY $`
请有人帮助我,我在stackoverflow中找不到任何答案。感谢。
答案 0 :(得分:2)
错误说:Parser期望一个JSON-Object,但它读取一个JSON数组。要修复它(如果您控制服务器),您应该将JSON字符串更改为以下内容:
{
"albums" : [
{
"id": "6",
"name": "King Stays King",
"artist_name":"Timbaland",
"image":""
},
{
"id": "7",
"name": "East Atlanta Santa 2",
"artist_name":"Gucci Mane",
"image":""
},
{
"id": "8",
"name": "The cuban connect",
"artist_name":"Phophit",
"image":""
},
{
"id": "9",
"name": "Shmoney Keeps",
"artist_name":"Calling",
"image":""
},
{
"id": "10",
"name": "Cabin Fever 3",
"artist_name":"Wiz khalifa",
"image":""
}
],
"paginationInfo" : {
"nextPage": "http://private-ede172-mymixtapez1.apiary-mock.com/features/page_3/",
"itemsTotal": 10,
"page": 2,
"pagerMax": 2
}
}
现在它是一个JSON-Object并且符合你的Java类。
如果您无法更改后端的JSON,我会将其作为行响应并使用GSON或手动分别解析albums Array
和PaginationInfo
。
顺便说一下。您必须在nextPage
类
int
类型从String
更改为PaginationInfo
答案 1 :(得分:0)
您的JSON在初始声明中遇到了麻烦。
根据json.org:
JSON基于两种结构:
•名称/值对的集合。在各种语言中,这是 实现为对象,记录,结构,字典,哈希表,键控 列表或关联数组。
•有序的值列表。多数情况 语言,这被实现为数组,向量,列表或序列。
尝试将您的JSON更新为:
"albums": [
{
"id": "6",
"name": "King Stays King",
"artist_name":"Timbaland",
"image":""
},
{
"id": "7",
"name": "East Atlanta Santa 2",
"artist_name":"Gucci Mane",
"image":""
},
{
"id": "8",
"name": "The cuban connect",
"artist_name":"Phophit",
"image":""
},
{
"id": "9",
"name": "Shmoney Keeps",
"artist_name":"Calling",
"image":""
},
{
"id": "10",
"name": "Cabin Fever 3",
"artist_name":"Wiz khalifa",
"image":""
}
],
"pagination_info":
{
"nextPage": "http://private-ede172-mymixtapez1.apiary-mock.com/features/page_3/",
"itemsTotal": 10,
"page": 2,
"pagerMax": 2
}