改造:预计BEGIN_ARRAY但是BEGIN_OBJECT

时间:2016-05-19 23:00:57

标签: retrofit2

正如我对get对象"服务" ?

{
"success": "true",
"code": "200",
"message": "The operation was successful",
"data": {
    "hairdressers": [
        {
            "name": "Jason Grant",
            "image_profile": "http:\/\/www.kbapi.co\/images\/header\/3.png",
            "open": "09:00:00",
            "close": "15:00:00",
            "id": "2",
            "id_saloon": "4",
            "services": [
                {
                    "name": "Corte",
                    "price": "8000",
                    "image": "http:\/\/www.kbapi.co\/images\/header\/1.png",
                    "time": "01:00:00",
                    "type": "W",
                    "saloon_id": "4",
                    "service_id": "3"
                },
                {
                    "name": "Corte",
                    "price": "8000",
                    "image": "http:\/\/www.kbapi.co\/images\/header\/1.png",
                    "time": "01:00:00",
                    "type": "C",
                    "saloon_id": "4",
                    "service_id": "4"
                },
                {
                    "name": "Corte",
                    "price": "9000",
                    "image": "http:\/\/www.kbapi.co\/images\/header\/1.png",
                    "time": "01:00:00",
                    "type": "O",
                    "saloon_id": "4",
                    "service_id": "5"
                }
            ]
        },
        {
            "name": "Herbert Davis",
            "image_profile": "http:\/\/www.kbapi.co\/images\/header\/4.png",
            "open": "10:00:00",
            "close": "16:00:00",
            "id": "3",
            "id_saloon": "4",
            "services": []
        }
    ]
}
}

2 个答案:

答案 0 :(得分:0)

发生错误是因为您告诉Retrofit您期望JSONArray,而是获得JSON object。我快速查看了您正在使用的结果,看起来它返回JSON object,然后返回的对象包含您有兴趣访问的列表。如果您发布GET来电,我可以为您提供进一步的帮助。

答案 1 :(得分:0)

你应该用另外一个java类包装你的对象列表

public class Response {
    private boolean success;
    private int code;
    private String message;
    private List<Data> data;
}

public class Data {
    private List<Hairdresser> hairdressers = new ArrayList<Hairdresser>();
}

要自动生成此对象,您可以使用服务like this one