2

我正在尝试将下面的这个 json 解析为一个名为的对象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;
}

分页信息类

public class PaginationInfo {
    int page;
    int pagerMax;
    int nextPage;
    int itemsTotal;
}

AlbumResponse,里面有两个类,并且AlbumList

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中没有找到任何答案。谢谢。

4

2 回答 2

2

错误说:解析器需要一个 JSON 对象,但它读取一个 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 对象并且符合您的 Java 类。

如果您无法在后端更改 JSON,我会将其作为行响应并分别使用 GSON 或手动解析和albums Array解析PaginationInfo

顺便提一句。您必须在课堂上将nextPage类型从int更改为StringPaginationInfo

于 2016-01-06T19:34:01.420 回答
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
}
于 2016-01-06T19:36:22.377 回答