2011-03-31 134 views
1

我用Gson解析了一些東西,但是Gson只識別json對象和數組。但我有這樣的事情:你知道如何解析這個嗎?

"results":[ 
    { 
     "nyt_movie_id":135855, 
     "display_title":"Big Night", 
     "sort_name":"Big Night", 
     "mpaa_rating":"R", 
     "critics_pick":1, 
     "thousand_best":"1", 
     "byline":"Janet Maslin", 
     "headline":"", 
     "capsule_review":"Restaurateur brothers stake all on one dinner. Succulent comedy.", 
     "summary_short":"", 
     "publication_date":"1996-03-29", 
     "opening_date":"1996-09-20", 
     "dvd_release_date":"1998-04-07", 
     "date_updated":"2010-08-25 14:47:08", 
     "seo_name":"Big-Night", 
     "link":{ 
      "type":"article", 
      "url":"http:\/\/movies.nytimes.com\/movie\/review?res=9501E6DA1539F93AA15750C0A960958260", 
      "suggested_link_text":"Read the New York Times Review of Big Night" 
     }, 
     "related_urls":[ 
      { 
       "type":"overview", 
       "url":"http:\/\/movies.nytimes.com\/movie\/135855\/Big-Night\/overview", 
       "suggested_link_text":"Overview of Big Night" 
      }, 
      { 
       "type":"showtimes", 
       "url":"http:\/\/movies.nytimes.com\/movie\/135855\/Big-Night\/showtimes", 
       "suggested_link_text":"Tickets & Showtimes for Big Night" 
      }, 
      { 
       "type":"awards", 
       "url":"http:\/\/movies.nytimes.com\/movie\/135855\/Big-Night\/details", 
       "suggested_link_text":"Cast, Credits & Awards for Big Night" 
      }, 
      { 
       "type":"community", 
       "url":"http:\/\/movies.nytimes.com\/movie\/135855\/Big-Night\/rnr", 
       "suggested_link_text":"Readers' Reviews of Big Night" 
      }, 
      { 
       "type":"trailers", 
       "url":"http:\/\/movies.nytimes.com\/movie\/135855\/Big-Night\/trailers", 
       "suggested_link_text":"Trailers & Clips for Big Night" 
      } 
     ] 
    } 

我的應用程序崩潰在「鏈接」,因爲我想使它像列表鏈接; 帶班鏈接看起來像這樣:

private String type; 
public void setType(); 

ECT 你能幫助我嗎?

我的結果類看起來是這樣的:

package com.galite.nyt; 

import java.util.List; 

public class Results { 
    private int movie_id; 
    private String display_title; 
    private String sort_name; 
    private String mpaa_rating; 
    private String critics_pick; 
    private String byline; 
    private String headline; 
    private String capsule_review; 
    private String summary_short; 
    private String publication_date; 
    private String opening_date; 
    private String dvd_release_date; 
    private String date_updated; 
    private List<RelatedUrl> related_url; 
    private String article; 
    private String url; 
    private String suggest_link_text; 
    //private List<Link> link; 

    public Results(){ 

    } 
    //Setters 
    public void setMovieId(int mi){ 
     movie_id = mi; 
    } 
    public void setDisplayTitle(String dp){ 
     display_title = dp; 
    } 
    public void setSortName(String sn){ 
     sort_name = sn; 
    } 
    public void setMpaaRating(String mr){ 
     mpaa_rating = mr; 
    } 
    public void setCriticsPick(String cp){ 
     critics_pick = cp; 
    } 
    public void setByline(String b){ 
     byline = b; 
    } 
    public void setHeadline(String h){ 
     headline = h; 
    } 
    public void setCapsuleReview(String cr){ 
     capsule_review = cr; 
    } 
    public void setSummaryShort(String ss){ 
     summary_short = ss; 
    } 
    public void setPublicationDate(String pd){ 
     publication_date = pd; 
    } 
    public void setOpeningDate(String op){ 
     opening_date = op; 
    } 
    public void setDvdReleaseDate(String drd){ 
     dvd_release_date = drd; 
    } 
    public void setDateUpdated(String du){ 
     date_updated = du; 
    } 
    public void setRelatedUrl(List<RelatedUrl> ru){ 
     related_url = ru; 
    } 
    //public void setLink(List<Link> l){ 
    // link = l; 
    //} 

    //Getters 
    public int getMovieId(){ 
     return movie_id; 
    } 
    public String getDisplayTitle(){ 
     return display_title; 
    } 
    public String getSortName(){ 
     return sort_name; 
    } 
    public String getMpaaRating(){ 
     return mpaa_rating; 
    } 
    public String getCriticsPick(){ 
     return critics_pick; 
    } 
    public String getByline(){ 
     return byline; 
    } 
    public String getHeadline(){ 
     return headline; 
    } 
    public String getCapsuleReview(){ 
     return capsule_review; 
    } 
    public String getSummaryShort(){ 
     return summary_short; 
    } 
    public String getPublicationDate(){ 
     return publication_date; 
    } 
    public String getOpeningDate(){ 
     return opening_date; 
    } 
    public String getDvdReleaseDate(){ 
     return dvd_release_date; 
    } 
    public String getDateUpdated(){ 
     return date_updated; 
    } 
    public List<RelatedUrl> getRelatedUrl(){ 
     return related_url; 
    } 
    //public List<Link> getLink(){ 
    // return link; 
    //} 
    //Setters 
    public void setArticle(String a){ 
     article=a; 
    } 
    public void setUrl(String u){ 
     url = u; 
    } 
    public void setSuggestLinkText(String s){ 
     suggest_link_text = s; 
    } 

    //Getters 
    public String getArticle(){ 
     return article; 
    } 
    public String getUrl(){ 
     return url; 
    } 
    public String getSuggestLinkText(){ 
     return suggest_link_text; 
    } 
} 

和我的鏈接類看起來是這樣的:

package com.galite.nyt; 

public class Link { 
    private String article; 
    private String url; 
    private String suggest_link_text; 
    public Link(){ 

    } 

    //Setters 
    public void setArticle(String a){ 
     article=a; 
    } 
    public void setUrl(String u){ 
     url = u; 
    } 
    public void setSuggestLinkText(String s){ 
     suggest_link_text = s; 
    } 

    //Getters 
    public String getArticle(){ 
     return article; 
    } 
    public String getUrl(){ 
     return url; 
    } 
    public String getSuggestLinkText(){ 
     return suggest_link_text; 
    } 
} 

就像你所看到的,我把鏈接列表中的註釋,因爲這是我收到的問題。 我試圖直接在Results類中傳遞鏈接。

回答

0

好吧問題是「鏈接」,它不是一個數組。什麼,你需要做的就是保持鏈接類,但在結果類添加:

private Link link; 

    public Link getLink(){ 
     return link; 
    } 
+0

哈。我只是發佈這個相同的答案。做好了解問題。還要注意你的語句「Gson只識別json對象和數組」是不正確的。 – 2011-06-08 09:33:44

+0

哈這就是我看到它並使用它:對於json對象和數組。但你是對的。 – Tsunaze 2011-06-08 10:39:25

0

一種方法是創建一個JSON響應的映射響應的對象結構相匹配的對象模型:

example: 
Reviews: 
    Review (Constructor from a JSONObject): 
    Fields - ID, title, name, bigNight, etc 
    List of Links 

Links (Constructor from a JSONArray): 
    Fields - Type, URL, etc 

然後創建了評價對象傳入審查響應 - 這創建所需的對象,並通過在JSONArray鏈接等...

+0

這是這個問題,我編輯我的問題,你可以在我的職業 – Tsunaze 2011-04-01 11:22:11

相關問題