从 Confluence JSON 响应中获取价值

Grabbing value from Confluence JSON response

我正在尝试从合流页面的 JSON 响应中获取 ID 字段,该数据如下所示:

`{"results":[{"id":"89425836","type":"page","status":"current","title":"string","extensions":{"position":"none"},"_links":{"webui":"/web/url","edit":"/pages/resumedraft.action?draftId=89425836","tinyui":"/x/rIdUBQ","self":"https://confluence.domain.org/rest/api/content/89425836"},"_expandable":{"container":"/rest/api/space/spaceName","metadata":"","operations":"","children":"/rest/api/content/89425836/child","restrictions":"/rest/api/content/89425836/restriction/byOperation","history":"/rest/api/content/89425836/history","ancestors":"","body":"","version":"","descendants":"/rest/api/content/89425836/descendant","space":"/rest/api/space/spaceName"}}],"start":0,"limit":25,"size":1,"_links":{"self":"https://confluence.domain.org/rest/api/content?spaceKey=spaceName&type=page&title=title","base":"https://confluence.domain.org","context":""}}`

我一直在网上查找,我的代码应该可以工作,但没有,我很茫然。

package main

import (
    "encoding/json"
    "net/http"
    "fmt"
    "io/ioutil"
)

type dataResponse struct {
    Id string `json:"id"`
}

func main() {

    var p dataResponse

    response, err := http.Get("https://confluence.domain.org/url/to/page/with/params"

    if err != nil {
       panic(err)
    } else {
      data, _ := ioutil.ReadAll(response.Body)
      json.Unmarshal(data, &p)
      fmt.Println(p.Id)
    }

   defer response.Body.Close()
}

但这并没有返回任何东西。我认为这与 "results" 之后的 id 字段有关,但还没有找到解决方法。编辑:更新了 url,不小心复制了调试一个。

id 在对象数组中,在 results 内。要解组 json 文档,您的结构必须与文档匹配,即:

type body struct {
  Results []dataResponse `json:"results"`
}

然后:

var doc body
json.Unmarshal(data, &doc)

您可以从以下位置获得 id

doc.Results[i].Id