0
我在Android應用中使用Retrofit 2與WordPress API進行通信,但我有問題從帖子獲取附件和標籤。從Wordpress API反序列化嵌套的JSON標籤和附件JSON響應
相應JSON響應看起來像這樣:
{
"ID": 1,
"site_ID": 1,
"author": {
},
"tags": {
"Doom": {
"ID": 654,
"name": "Doom",
"slug": "doom",
"description": "",
"post_count": 53
},
"Ego-Shooter": {
"ID": 73,
"name": "Ego-Shooter",
"slug": "ego-shooter",
"description": "",
"post_count": 724
},
"id Software": {
"ID": 127,
"name": "id Software",
"slug": "id-software",
"description": "",
"post_count": 41
}
}
"attachments": {
"54344": {
"ID": 54344,
"URL": "",
"guid": "",
"mime_type": "image/jpeg",
"width": 843,
"height": 499
},
"54345": {
"ID": 54345,
"URL": "",
"guid": "",
"mime_type": "image/jpeg",
"width": 800,
"height": 1600
}
}
}
Post.class:
public class Post {
@SerializedName("ID")
private int ID;
@SerializedName("title")
private String title;
@SerializedName("content")
private String content;
@SerializedName("featured_image")
private String featuredImage;
@SerializedName("date")
private String date;
@SerializedName("URL")
private String URL;
@SerializedName("author")
private Author author;
@SerializedName("discussion")
private Discussion discussion;
@SerializedName("attachments")
private Attachment attachments; // At this point I have problems
}
Attachment.class
public class Attachment {
@SerializedName("URL")
private String URL;
@SerializedName("width")
private int width;
@SerializedName("height")
private int height;
}
除附件或標籤中嵌套的JSON對象外,一切正常。附件對象只包含默認值,並且沒有從JSON響應中填充正確的值。
我改造建設者:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(WordPressService.ENDPOINT)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
return retrofit.create(WordPressService.class);
我想有附着對象的列表,但我不知道我怎麼能解決這個問題。
顯然是因爲'attachments'不是'Attachment'但附件 – Selvin
的,而關聯數組是沒有附件的陣列,請參閱JSON響應 –
** **聯想來自wiki的數組*在計算機科學中,關聯數組,映射,符號表或字典是由**(鍵,值)對**集合組成的抽象數據類型,以便每個可能的鍵最多出現一次收集。* – Selvin