2016-01-06 65 views
2

我想解析這個json波紋管,到名爲AlbumResponse的對象裏面有另外兩個對象AlbumPaginationInfo。改造版本2.0無法解析json和兩種不同的數據類型在改進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 
} 

Album類

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,但在BEGIN_ARRAY第1行第2列路徑$`

請有人可以幫助我,我沒有找到任何答案在stackoverflow。謝謝。

回答

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 
} 
+0

是的,它的工作就像一個魅力,問題是在'JSON',而不是'RETROFIT'。這是一個begginer的錯誤。謝謝 – diogojme

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 ArrayPaginationInfo

Btw。您必須將nextPage類型從int更改爲String中的PaginationInfo

+0

是的,它完美的作品。 json在我的服務器中是靜態的,我犯了這個錯誤。謝謝。 – diogojme

+0

好!這將是很好的答案接受:) – MoQ93