【问题标题】:Cannot parse json with two different data types in retrofit 2.0在改造 2.0 中无法解析具有两种不同数据类型的 json
【发布时间】:2016-01-06 19:13:24
【问题描述】:

我正在尝试将下面的这个 json 解析为一个名为 AlbumResponse 的对象,其中包含另外两个对象,AlbumPaginationInfo。改造 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,里面有两个类,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中没有找到任何答案。谢谢。

【问题讨论】:

    标签: android json retrofit retrofit2


    【解决方案1】:

    错误提示:解析器需要一个 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 ArrayPaginationInfo

    顺便说一句。您必须在 PaginationInfo 类中将 nextPage 类型从 int 更改为 String

    【讨论】:

    • 是的,它运行良好。 json 在我的服务器中是静态的,我犯了这个错误。谢谢。
    【解决方案2】:

    您的 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
    }
    

    【讨论】:

    • 是的,它就像一个魅力,问题在于JSON,而不是RETROFIT。这是一个初学者的错误。谢谢
    猜你喜欢
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2014-11-28
    相关资源
    最近更新 更多