从Confluence JSON响应中获取价值

时间:2019-10-21 13:11:53

标签: 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字段有关,但是还没有找到一种方法来处理。编辑:更新URL,无意间复制了调试文件。

1 个答案:

答案 0 :(得分:0)

db.getCollection("Students").find({},{name1:1,last_name1:1,studies1:1,_id:0}) id内部的对象数组中。要解组json文档,您的结构必须与该文档匹配,即:

results

然后:

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

您可以从以下位置获取var doc body json.Unmarshal(data, &doc)

id