2013-11-21 56 views
1

我正在facebook應用程序上工作。當我查詢https://graph.facebook.com/me/video.watches?offset=0&limit=1000時,我收到了觀看電影列表。例如。我在這裏只粘貼了一部電影。Json結構是如此不同無法將其改爲Java對象

{ 
    "data": [ 
    { 
     "id": "664878940211923", 
     "data": { 
     "tv_show": { 
      "id": "108611845829948", 
      "url": "https://www.facebook.com/pages/Popeye-the-Sailor/108611845829948", 
      "type": "video.tv_show", 
      "title": "Popeye the Sailor" 
     } 
     }, 
     "type": "video.watches", 
    }, 

這是我創建的POJO類,用於將其轉換爲Java。

import java.util.List; 

public class FBUserVideoWatches { 
    private List<Data> data; 

    public List<Data> getData() { 
     return data; 
    } 
    public void setData(List<Data> data) { 
     this.data = data; 
    } 
    public class Data{ 
     private long id; 
     private TVData data; 
     private String type; 

     public long getId() { 
      return id; 
     } 
     public void setId(long id) { 
      this.id = id; 
     } 
     public TVData getData() { 
      return data; 
     } 
     public void setData(TVData data) { 
      this.data = data; 
     } 
     public String getType() { 
      return type; 
     } 
     public void setType(String type) { 
      this.type = type; 
     } 
    } 

    public class TVData{ 
     private TV_Shows tv_shows; 

     public TV_Shows getTv_shows() { 
      return tv_shows; 
     } 

     public void setTv_shows(TV_Shows tv_shows) { 
      this.tv_shows = tv_shows; 
     } 
    } 
    public class TV_Shows{ 
     private long id; 
     private String url; 
     private String type; 
     private String title; 

     public long getId() { 
      return id; 
     } 
     public void setId(long id) { 
      this.id = id; 
     } 
     public String getUrl() { 
      return url; 
     } 
     public void setUrl(String url) { 
      this.url = url; 
     } 
     public String getType() { 
      return type; 
     } 
     public void setType(String type) { 
      this.type = type; 
     } 
     public String getTitle() { 
      return title; 
     } 
     public void setTitle(String title) { 
      this.title = title; 
     } 
    } 
} 

這是我如何將json轉換爲java。

FBUserVideoWatches fbUserVideoWatches = gson.fromJson(response.getBody(), FBUserVideoWatches.class); 

      for (Data data : fbUserVideoWatches.getData()) { 
       System.out.println(data.getId());//This only works and I am getting values. 
       if(null != data.getData()){ 
        if(null!=data.getData().getTv_shows()){ 
         System.out.print(data.getData().getTv_shows().getTitle()); 
        } 
        if(null!=data.getData().getTv_shows()){ 
         System.out.print(data.getData().getTv_shows().getType()); 
        } 
       } 
      } 

當我使用getter方法得到Java類數據我得到ID 664878940211923 & "type": "video.watches"如上圖所示。 "tv_show"裏面的成員我無法訪問。我認爲在創建POJO時出錯了。我無法找到那個錯誤。請幫助我做什麼修正是必要的,以使這項工作。希望我的問題很明確。提前致謝。

+0

你可以只是簡單的打印'fbUserVideoWatches'第一行後檢查JSON轉換後的身份。 –

+0

問題不是我懷疑的。因爲我可以在解析Json之後從java打印data.getId()。我認爲類TVTV和TV_Shows類與Json映射不正確的問題,我認爲。 –

+0

我檢查了gson文檔,我認爲問題是'FBUserVideoWatches'的集合成員,我想[JsonParser](https://code.google.com/p/google-gson/source/browse/trunk/extras /src/main/java/com/google/gson/extras/examples/rawcollections/RawCollectionsExample.java)可能會給你一些提示。 –

回答

2

你在TVData類的實例屬性的不同影響在JSON和電視數據類

@SerializedName(value="tv_show") 
private TV_Shows tv_shows; 

做錯誤你忘了系列化的名字怎麼把它假設你的JSON是這樣

{ 
"data":[ 
     { 
     "id":"664878940211923", 
     "data":{ 
      "tv_show":{ 
       "id":"108611845829948", 
       "url":"https://www.facebook.com/pages/Popeye-the-Sailor/108611845829948", 
       "type":"video.tv_show", 
       "title":"Popeye the Sailor" 
      } 
     }, 
     "type":"video.watches" 
     }, 
     { 
     "id":"664878940211923", 
     "data":{ 
      "tv_show":{ 
       "id":"108611845829948", 
       "url":"https://www.facebook.com/pages/Popeye-the-Sailor/108611845829948", 
       "type":"video.tv_show", 
       "title":"Popeye the Sailor" 
      } 
     }, 
     "type":"video.watches" 
     } 
    ] 
} 

解析模型會喜歡下面根據你的你的json格式

public class FBUserVideoWatches { 

    private List<Data> data; 

    public List<Data> getData() { return data; } 

    public void setData(List<Data> data) { this.data = data;} 
} 


public class Data { 

    private TVData data; 

    private String id; 
    private String type; 

// setter/getter here 
} 

public class TVData { 

    @SerializedName(value="tv_show") 
    private Show show; 
// setter/getter here 
} 

public class Show { 

    private String id; 
    private String url; 
    private String type; 
    private String title; 
     // setter/getter here 
    } 

最終與谷歌GSON解析你的對象像下面

Gson gson = new Gson(); 
FBUserVideoWatches fbUserVideoWatches =gson.fromJson(json_string, FBUserVideoWatches.class); 
+0

謝謝....其工作。 –

相關問題