代码之家  ›  专栏  ›  技术社区  ›  calmeishmael

解组复杂Json

  •  1
  • calmeishmael  · 技术社区  · 10 年前

    我最近刚开始使用API和http请求,我正在尝试构建一个应用程序,使用Reddit API在特定的子Reddit上发布帖子。

    这是我正在练习的带有json和搜索参数的页面: https://www.reddit.com/r/hiphopheads.json?limit=1

    查看Golang的JSON模块的标准库,我仍然不知道如何使用JSON。这个复杂JSON的解组。根据我收集的信息,我必须定义一个类似于JSON结构的结构来实际保存数据

    我在这个网站上发布了链接,以了解JSON的实际结构: https://jsonformatter.curiousconcept.com/

    现在,我想要的主要内容是标题,它位于数据->下;儿童->;数据->;标题如果我想将JSON解组到一个对象中,我需要定义一个嵌套结构对象吗?或者有没有更简单的方法来实现这一点,这样我就不必计算JSON的所有属性并自己定义它们??

    非常感谢任何能让我走上正轨的帮助。谢谢

    2 回复  |  直到 10 年前
        1
  •  2
  •   jussius    10 年前

    您不必在结构中定义不需要的字段。解组将仅解码结构中存在的值。但不幸的是,对于嵌套JSON,您还必须定义所有父字段(不像在xml包中,您可以在标记中定义路径)。因此,您的结构可以如下所示:

    type Foo struct {
        Data struct {
            Children []struct {
                Data struct {
                    Title string
                }
            }
        }
    }
    

    有关工作示例,请参见此处: https://play.golang.org/p/UeUYfWBONL

        2
  •  0
  •   Not_a_Golfer    10 年前

    似乎您试图解组的JSON过于复杂,因此您的结构必须复杂,但这就是它的方式。

    有一些工具可以从JSON生成结构定义,这节省了大量工作。使用您发布的JSON和 such an online tool ,我生成了以下结构:

    package main
    
    type MyJsonName struct {
        Data struct {
            After    string      `json:"after"`
            Before   interface{} `json:"before"`
            Children []struct {
                Data struct {
                    ApprovedBy          interface{}   `json:"approved_by"`
                    Archived            bool          `json:"archived"`
                    Author              string        `json:"author"`
                    AuthorFlairCSSClass string        `json:"author_flair_css_class"`
                    AuthorFlairText     interface{}   `json:"author_flair_text"`
                    BannedBy            interface{}   `json:"banned_by"`
                    Clicked             bool          `json:"clicked"`
                    ContestMode         bool          `json:"contest_mode"`
                    Created             int           `json:"created"`
                    CreatedUtc          int           `json:"created_utc"`
                    Distinguished       string        `json:"distinguished"`
                    Domain              string        `json:"domain"`
                    Downs               int           `json:"downs"`
                    Edited              bool          `json:"edited"`
                    Gilded              int           `json:"gilded"`
                    Hidden              bool          `json:"hidden"`
                    HideScore           bool          `json:"hide_score"`
                    ID                  string        `json:"id"`
                    IsSelf              bool          `json:"is_self"`
                    Likes               interface{}   `json:"likes"`
                    LinkFlairCSSClass   string        `json:"link_flair_css_class"`
                    LinkFlairText       string        `json:"link_flair_text"`
                    Locked              bool          `json:"locked"`
                    Media               interface{}   `json:"media"`
                    MediaEmbed          struct{}      `json:"media_embed"`
                    ModReports          []interface{} `json:"mod_reports"`
                    Name                string        `json:"name"`
                    NumComments         int           `json:"num_comments"`
                    NumReports          interface{}   `json:"num_reports"`
                    Over18              bool          `json:"over_18"`
                    Permalink           string        `json:"permalink"`
                    Quarantine          bool          `json:"quarantine"`
                    RemovalReason       interface{}   `json:"removal_reason"`
                    ReportReasons       interface{}   `json:"report_reasons"`
                    Saved               bool          `json:"saved"`
                    Score               int           `json:"score"`
                    SecureMedia         interface{}   `json:"secure_media"`
                    SecureMediaEmbed    struct{}      `json:"secure_media_embed"`
                    Selftext            string        `json:"selftext"`
                    SelftextHTML        string        `json:"selftext_html"`
                    Stickied            bool          `json:"stickied"`
                    Subreddit           string        `json:"subreddit"`
                    SubredditID         string        `json:"subreddit_id"`
                    SuggestedSort       interface{}   `json:"suggested_sort"`
                    Thumbnail           string        `json:"thumbnail"`
                    Title               string        `json:"title"`
                    Ups                 int           `json:"ups"`
                    URL                 string        `json:"url"`
                    UserReports         []interface{} `json:"user_reports"`
                    Visited             bool          `json:"visited"`
                } `json:"data"`
                Kind string `json:"kind"`
            } `json:"children"`
            Modhash string `json:"modhash"`
        } `json:"data"`
        Kind string `json:"kind"`
    }
    

    通常,这些工具的输出仍然需要手动调整才能正常工作。例如:

    MediaEmbed          struct{}      `json:"media_embed"`
    

    我很确定这不是这里需要的。但它在展示基本思想和正确理解大部分内容方面确实有很大的帮助。您可以尝试其他类似的工具。