Ich verwende Retrofit 2 in einer Android-App, um mit WordPress API zu kommunizieren, aber ich habe Probleme, die Anhänge und Tags von den Beiträgen zu erhalten.Deserialize verschachtelte JSON-Tags und Anhänge von Wordpress API JSON Antwort
Die entsprechende JSON-Antwort sieht wie folgt aus:
{
"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;
}
Alles funktioniert gut außer den geschachtelten JSON-Objekten in Anlagen oder Tags. Das Anlageobjekt enthält nur Standardwerte und ist nicht mit den richtigen Werten aus der JSON-Antwort gefüllt.
Mein Retrofit Bauer:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(WordPressService.ENDPOINT)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
return retrofit.create(WordPressService.class);
Ich möchte eine Liste der Befestigungs Objekte haben, aber ich weiß nicht, wie ich dieses Problem lösen kann.
offenbar, weil 'attachments' nicht' Attachment' ist, sondern eher assoziatives Array von Attachments – Selvin
Es ist kein Array von Attachments, JSON-Antwort –
** assoziativen siehe ** Array aus Wiki * In der Informatik ist ein assoziatives Array, eine Map, eine Symboltabelle oder ein Dictionary ein abstrakter Datentyp, der aus einer ** Sammlung von (Schlüssel, Wert) -Paaren ** besteht, so dass jeder mögliche Schlüssel höchstens einmal erscheint die Sammlung. * – Selvin