反序列化来自 Wordpress API JSON 响应的嵌套 JSON 标签和附件
Deserialize nested JSON tags and attachments from Wordpress API JSON response
我在 Android 应用程序中使用 Retrofit 2 与 WordPress API 通信,但我无法从帖子中获取附件和标签。
相应的 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 对象外,一切正常。 Attachment 对象仅包含默认值,未填充 JSON 响应中的正确值。
我的 Retrofit 构建器:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(WordPressService.ENDPOINT)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
return retrofit.create(WordPressService.class);
我想要一个附件对象列表,但我不知道如何解决这个问题。
要解析 attachments
和 tags
JSON
元素,请使用 LinkedTreeMap
.
更新 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("tags")
LinkedTreeMap<String, Tag> tags;
@SerializedName("attachments")
LinkedTreeMap<String, Attachment> attachments;
}
我在 Android 应用程序中使用 Retrofit 2 与 WordPress API 通信,但我无法从帖子中获取附件和标签。
相应的 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 对象外,一切正常。 Attachment 对象仅包含默认值,未填充 JSON 响应中的正确值。
我的 Retrofit 构建器:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(WordPressService.ENDPOINT)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
return retrofit.create(WordPressService.class);
我想要一个附件对象列表,但我不知道如何解决这个问题。
要解析 attachments
和 tags
JSON
元素,请使用 LinkedTreeMap
.
更新 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("tags")
LinkedTreeMap<String, Tag> tags;
@SerializedName("attachments")
LinkedTreeMap<String, Attachment> attachments;
}