Go中的嵌套JSON反序列化

时间:2018-07-17 15:47:44

标签: json go nested unmarshalling

我正在尝试将嵌套的JSON响应转换为结构。该结构如下所示:

type PullRequestEvent struct {
    Action               string   `json:"action"`
    Number               int      `json:"number"`
    PullRequest          struct { 
        Url              string   `json:"url"`
        HtmlUrl          string   `json:"html_url"`
        DiffUrl          string   `json:"diff_url"` 
        Title            string   `json:"title_url"` 
    } `json:"pull_request"`
}

但是,仅解析顶级字段(ActionNumber);其余的保留为nil或0。转换代码如下:

func whatever(w http.ResponseWriter, r *http.Request) {
    var ev PullRequestEvent
    dec := json.NewDecoder(r.Body)
    err := dec.Decode(&ev)
    if err != nil {
        // fail request 
    }
}

JSON是拉取请求事件here。相关部分是

{   
    "action": "closed",   "number": 1,   "pull_request": {
    "url": "https://api.github.com/repos/Codertocat/Hello-World/pulls/1",
    "id": 191568743,
    "node_id": "MDExOlB1bGxSZXF1ZXN0MTkxNTY4NzQz",
    "html_url": "https://github.com/Codertocat/Hello-World/pull/1",
    "diff_url": "https://github.com/Codertocat/Hello-World/pull/1.diff",
    "number": 1,
    "state": "closed",
    "locked": false,
    "title": "Update the README with new information",
    "user": {
      "login": "Codertocat",
      "id": 21031067,
      "node_id": "MDQ6VXNlcjIxMDMxMDY3",
      "gravatar_id": "",
      "url": "https://api.github.com/users/Codertocat",
      "html_url": "https://github.com/Codertocat",
      "subscriptions_url": "link",
      "organizations_url": "link",
      "repos_url": "link",
      "events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
      "received_events_url": "link",
      "type": "User",
      "site_admin": false
    }
    //...  }

这里的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

您发布的代码没有问题,只是您期望的响应中没有字段title_url(您可能只需要title)。 Here's一个展示它的游乐场示例。

package main

import (
    "bytes"
    "encoding/json"
    "log"
)

const examplePayload = `
{
  "action": "closed",
  "number": 1,
  "pull_request": {
    "url": "https://api.github.com/repos/Codertocat/Hello-World/pulls/1",
    "id": 191568743,
    "node_id": "MDExOlB1bGxSZXF1ZXN0MTkxNTY4NzQz",
    "html_url": "https://github.com/Codertocat/Hello-World/pull/1",
    "diff_url": "https://github.com/Codertocat/Hello-World/pull/1.diff",
    "patch_url": "https://github.com/Codertocat/Hello-World/pull/1.patch",
    "issue_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/1",
    "number": 1,
    "state": "closed",
    "locked": false,
    "title": "Update the README with new information"
  }
}
`

type PullRequestEvent struct {
    Action      string `json:"action"`
    Number      int    `json:"number"`
    PullRequest struct {
        Url     string `json:"url"`
        HtmlUrl string `json:"html_url"`
        DiffUrl string `json:"diff_url"`
        Title   string `json:"title"`
    } `json:"pull_request"`
}

func main() {
  var ev PullRequestEvent
    ex := bytes.NewReader([]byte(examplePayload))
    if err := json.NewDecoder(ex).Decode(&ev); err != nil {
        log.Fatal(err)
    }
    log.Printf("%#v", ev)
}