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

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

如何讀取json格式的數據?

如何讀取json格式的數據?

Go
拉莫斯之舞 2023-05-15 15:07:36
我有一個函數,我必須將數據以 json 格式發布到 url。當我發送數據時,它會以 json 格式給出響應。但它會向我展示:-代碼給出的輸出&{200 OK 200 HTTP/1.1 1 1 map[Content-Type:[application/json] X-Request-Id:[CgiFzq669pAYzRABGBAiCQiRtaznvJffAg] Keep-Alive:[timeout=60] Vary:[Accept-Encoding] X-Content-Type-Options:[nosniff] X-Download-Options:[noopen] X-Permitted-Cross-Domain-Policies:[none] Strict-Transport-Security:[max-age=631152000] X-Frame-Options:[DENY] X-Xss-Protection:[1; mode=block] Date:[Tue, 11 Dec 2018 09:35:22 GMT] Access-Control-Allow-Headers:[Content-Type, Authorization, Accept] Access-Control-Allow-Origin:[*] Access-Control-Expose-Headers:[Link]] 0xc420442080 -1 [] false true map[] 0xc42023e100 0xc4200e0d10}代碼是:-func Token(c *gin.Context) {   code := c.Query("code")   responseToken :=TokenResponse{}   token := models.PostToken{     ClientID:     "appllication Id",     ClientSecret: "applicationSecreteId",     Code:         "code",     RedirectUri:  c.Request.Host + c.Request.URL.RequestURI(),   }   bindData, err := json.Marshal(token)   if err != nil {     panic(err)   }   var jsonStr = []byte(string(bindData))   url :="https://connect.squareup.com/oauth2/token"   req, err := http.Post(url, "application/json", bytes.NewBuffer(jsonStr))   fmt.Println(req, err)}type TokenResponse struct {  Token      string `json:"access_token"`  Type       string `json:"token_type"`  ExpiresAt  string `json:"expires_at"`  MerchantId string `json:"merchant_id"`}預期輸出:-{  "access_token": "token",  "token_type": "bearer",  "expires_at": "2019-01-10T08:20:59Z",  "merchant_id": "id"}但是當我在郵遞員中點擊“ https://connect.squareup.com/oauth2/token ” url 時它會給我 json 但在 golang 代碼中它不會顯示任何 json 它將返回上述數據。誰能告訴我如何從上面的響應中獲取 json 數據?
查看完整描述

2 回答

?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

嘗試這個,


respBody, Err := ioutil.ReadAll(req.Body)


fmt.Println(string(respBody))


var temp TokenResponse


err := json.Unmarshal(respBody, &temp)


查看完整回答
反對 回復 2023-05-15
?
森林海

TA貢獻2011條經驗 獲得超2個贊

您必須像這樣閱讀響應數據:


body, err := ioutil.ReadAll(req.Body)

response := map[string]interface{}

json.Unmarshal(body,&response)


查看完整回答
反對 回復 2023-05-15
  • 2 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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