2016-08-09 2 views
0

WordPress APIと通信するためにAndroidアプリでRetrofit 2を使用していますが、投稿から添付ファイルやタグを取得する際に問題があります。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オブジェクト以外はすべて正常に動作します。 Attachmentオブジェクトにはデフォルト値しか含まれておらず、JSONレスポンスの正しい値で埋められません。

私のレトロフィットビルダー:

Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(WordPressService.ENDPOINT) 
      .client(okHttpClient) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) 
      .build(); 
    return retrofit.create(WordPressService.class); 

私は、添付ファイルのオブジェクトのリストを持っていると思いますが、私は、この問題を解決することができるのか分かりません。

+2

明らか 'ので' Attachment'が、添付ファイル – Selvin

+0

のではなく連想配列は、それが添付ファイルのない配列は、JSONレスポンスを参照、ではありません –

+1

**連想** Wikiの配列*コンピュータサイエンスでは、連想配列、マップ、シンボルテーブル、または辞書は、(キー、値)のペア**からなる抽象的なデータ型です。コレクション。* – Selvin

答えて

2

attachmentstagsJSONの要素を解析するには、LinkedTreeMapを使用します。

更新Postクラスは、次のようにattachments`ではない

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; 
}  
関連する問題