将具有嵌套数组和对象的JSON数据解组到Go结构中

Ook*_*Ook 1 youtube arrays json go unmarshalling

我通过使用从Youtube API接收的数据,将Youtube json响应解组到Go结构中,如下所示:

{
 "kind": "youtube#searchListResponse",
 "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/5xHRkUxevhiDF1huCnKw2ybduyo\"",
 "nextPageToken": "CBQQAA",
 "regionCode": "TH",
 "pageInfo": {
  "totalResults": 36,
  "resultsPerPage": 20
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/aMbszoNudZchce3BIjZC_YemugE\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "fvh6CQ7FxZE"
   },
   "snippet": {
    "publishedAt": "2016-07-16T14:42:36.000Z",
    "channelId": "UCuX4iswo8acMxDNcbrceRYQ",
    "title": "Japan ?????????:???????????? 2 ????????????????????????#ramen",
    "description": "Ramen Kio ??????????????????????????????????????????????????????????????...",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/fvh6CQ7FxZE/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/fvh6CQ7FxZE/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/fvh6CQ7FxZE/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "Japan aroi sudsud TV",
    "liveBroadcastContent": "none"
   }
  }
 ]
}
Run Code Online (Sandbox Code Playgroud)

为此,我在Go中为此创建了一个结构

type YoutubeData struct {
    Kind          string `json:"kind"`
    Etag          string `json:"etag"`
    NextPageToken string `json:"nextPageToken"`
    RegionCode    string `json:"regionCode"`
    PageInfo      struct {
        TotalResults   string `json:"totalResults"`
        ResultsPerPage string `json:"resultsPerPage"`
    } `json:"pageInfo"`
    Items []struct {
        Kind string `json:"kind"`
        Etag string `json:"etag"`
        Id   struct {
            Kind    string `json:"kind"`
            VideoId string `json:"videoId"`
        } `json:"id"`
        Snippet struct {
            PublishedAt string `json:"publishedAt"`
            ChannelId   string `json:"channelId"`
            Title       string `json:"title"`
            Description string `json:"description"`
            Thumbnails  struct {
                Default struct {
                    Url    string `json:"url"`
                    Width  string `json:"width"`
                    Height string `json:"height"`
                } `json:"default"`
                Medium struct {
                    Url    string `json:"url"`
                    Width  string `json:"width"`
                    Height string `json:"height"`
                } `json:"medium"`
                High struct {
                    Url    string `json:"url"`
                    Width  string `json:"width"`
                    Height string `json:"height"`
                } `json:"high"`
            } `json:"thumbnails"`
            ChannelTitle         string `json:"channelTitle"`
            LiveBroadcastContent string `json:"liveBroadcastContent"`
        } `json:"snippet"`
    } `json:"items"`
}
Run Code Online (Sandbox Code Playgroud)

我通过使用这种方法将其拆封

youtubeData := YoutubeData{}

if json.Unmarshal(b, &youtubeData); err != nil {

} else {

} 
Run Code Online (Sandbox Code Playgroud)

其中b是从Youtube API接收的字节数据。我成功将字节对象中的所有数据打印到控制台后,将它们解组并尝试使用{{。}}在模板上输出后,我收到了

{youtube#searchListResponse "5g01s4-wS2b4VpScndqCYc5Y-8k/JwGY0TWwWswjZ9LOvemaF5yxsMo" CBQQAA TH { } []}
Run Code Online (Sandbox Code Playgroud)

除json对象和数组中的数据(即pageInfoitems)外,所有数据均未编组。这里只是空白。我相信我都正确导出了它们。当涉及到JSON解组时,是否还有其他步骤将数据放入切片或嵌套在Go中另一个结构内的结构中?

Ook*_*Ook 5

我发现当我使用自动生成的json从

https://mholt.github.io/json-to-go/

现在,它可以完美运行了。我再也不会手工手工制作了。