json - 从 Confluence JSON 响应中获取值(value)

标签 json go confluence-rest-api

我正在尝试从汇合页面的 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()
}

但这并没有返回任何东西。我认为这与 id 字段在“结果”之后有关,但还没有找到解决方法。编辑:更新的网址,不小心复制了调试。

最佳答案

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

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

然后:
var doc body
json.Unmarshal(data, &doc)

你可以得到id从:
doc.Results[i].Id

关于json - 从 Confluence JSON 响应中获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58487054/

相关文章:

jquery - 打印json对象的key和value

go - 为什么我可以为变量分配不同类型的值?

go - 有没有相当于Python的ChainMap的golang包?

type-conversion - 无法将 [] 字符串转换为 [] 接口(interface) {}

confluence - 解释来自 Confluence REST API 的权限

json - 使用 map 将 JSON 数组解析为矩阵

jquery - 如何使用 jQuery/JSON/oembed 从 URL 嵌入推文

javascript - Klipfolio 数据与 Highchart Javascript 集成

Java REST API 用于使用 Atlassian Confluence 中的内容创建和更新新页面‎

jquery - 使用 jquery 将页面添加到 confluence