你知道如何解析这个吗?

发布于 2024-10-28 01:15:39 字数 6300 浏览 0 评论 0原文

我有一些东西需要用 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();

等 你能帮助我吗?

我的结果类看起来像这样:

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;
    }
}

就像你可以看到的,我将列表链接放在评论中,因为这是我收到的问题。 我尝试直接在结果类中传递链接。

I've got somthing to parse with Gson, but Gson recognize only json object and array. But i've got something like this:

"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"
            }
        ]
    }

My app crashes at "link" because i wanted to make it like List links;
with class Links looking like this:

private String type;
public void setType();

ect
Can you help me?

My Result class looks like this:

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;
    }
}

and my link class looks like this:

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;
    }
}

Like you can see i put List links in comments because it was the problem i received.
And i tried to pass link directly in the Results class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小矜持 2024-11-04 01:15:39

一种方法是创建一个与将响应映射到对象的 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

然后创建传入评论响应的 Reviews 对象 - 它会创建所需的对象并传入 JSONArray 来链接ETC...

One approach would be to create an object model that matches the structure of the JSON response that maps the response to the objects:

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

Then you create the Reviews object passing in the reviews response - and it creates the needed objects and passes in the JSONArray to link etc...

送君千里 2024-11-04 01:15:39

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

   private Link link;

   public Link getLink(){
       return link;
   }

Okay the problem was "link", it's not an array . What you need to do is keep the Link class but in the Results class add:

   private Link link;

   public Link getLink(){
       return link;
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文