亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Go 中通過 http 解組 JSON

如何在 Go 中通過 http 解組 JSON

Go
米琪卡哇伊 2021-06-18 13:20:52
我正在訪問一個以以下形式返回 JSON 的 API:[{"UniqueID":1234, "DocID":5678}, {"UniqueID":5678, "DocID":9101112}]這個 API 是用 Go 編寫的,示例輸出是返回在瀏覽器中的顯示方式。內容類型標頭是 application/json我有以下代碼來檢索和解組它:    type UniqueIDDocID struct{        UniqueID int64 `json: "UniqueID"`        DocID int64 `json: "DocID"`    }    type UniqueIDDocIDCollection struct{        FullList []UniqueIDDocID    }    func retrieveUniqueIDByPublication(nodeid int64, publication string){        // call the API        //hgr := new(retrieval.HttpGetRetrieval)// endpoint is defined here - I have removed for privacy reasons        fmt.Println(endpoint) // the url which I know works        /*response, err := hgr.RequestResponse(endpoint)            if err != nil {                l4g.Warn("Could not retrieve the endpoint %s. Error: ", endpoint, err)                return            }            defer hgr.CloseResponse(response)            queryResult := hgr.ReadResponse(response)*/            client := new(http.Client)            request, err := http.NewRequest("GET", endpoint, nil)            if err != nil {                l4g.Warn("Could not retrieve the endpoint %s. Error: %v", endpoint, err)                return              }            request.Header.Set("Content-Type", "applicaiton/json")            response, err := client.Do(request)            if err != nil {                l4g.Warn("Could not retrieve the endpoint %s. Error: %v", endpoint, err)                return              }我收到“無法解組”消息,日志中的詳細信息顯示如下:[91 123 34 85 110 105 113 117 101 73 68 34 58 34 56 51     57 51 50 53 56 54 34 44 34 68 111 99 73 68 34 58 52 49 50 49 54 57 49 57 125 44 123 34 85 110 105 113 117 101]作為代表性樣本。在這個解組步驟中我做錯了什么?我想要的輸出是 UniqueIDDocIDCollection 結構上有一個切片,其中包含類型 UniqueIDDocID 的項目,然后我可以從中獲取 UniqueID 并將其寫入行分隔文件。我一直在谷歌搜索并嘗試了很多東西,但每次都是我得到的。如果您對源 JSON 也有任何建議,請分享這些建議,因為我可以在 API 中進行更改。
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 222 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號