你知道如何解析这个吗?

时间:2011-03-31 22:01:35

标签: android json parsing gson

我已经有了与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;
    }
}

就像你可以看到我在评论中放入List链接,因为这是我收到的问题。 我试图直接在Results类中传递链接。

2 个答案:

答案 0 :(得分: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以链接等...

答案 1 :(得分:0)

好的问题是“链接”,它不是数组。您需要做的是保留Link类,但在Results类中添加:

   private Link link;

   public Link getLink(){
       return link;
   }