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

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

嘗試解碼 json 的主體時出現錯誤“data1.Body undefined”

嘗試解碼 json 的主體時出現錯誤“data1.Body undefined”

Go
侃侃無極 2022-10-17 16:42:56
所以我再次嘗試獲取這些數據,但它返回一個錯誤data.Body undefined (type []byte has no field or method Body)在此代碼的第 16 和 23 行。所以當它解碼 json 如果有人可以幫助我,這是我的代碼func SkyblockActiveAuctions() (structs.SkyblockActiveAuctions, error) {    var auctions structs.SkyblockActiveAuctions    startTime := time.Now()    statusCode, data, err := fasthttp.Get(nil, "https://api.hypixel.net/skyblock/auctions")    if err != nil {        return auctions, err    }    fmt.Println(statusCode)    var totalPages = auctions.TotalAuctions    for i := 0; i < totalPages; i++ {        statusCode, data1, err := fasthttp.Get(nil, "https://api.hypixel.net/skyblock/auctions")        if err != nil {            return auctions, err        }        fmt.Println(statusCode)        json.NewDecoder(data1.Body).Decode(&auctions)        fmt.Println(auctions.LastUpdated)    }    endTime := time.Now()    var timeTook = endTime.Sub(startTime).Milliseconds()    fmt.Println(data)    json.NewDecoder(data.Body).Decode(&auctions)    fmt.Println(auctions.LastUpdated)    fmt.Println(timeTook)    return auctions, err}
查看完整描述

1 回答

?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

  json.NewDecoder(data.Body).Decode(&auctions)
data.Body undefined (type []byte has no field or method Body)

data已經是響應的正文了。

json.NewDecoder期望 anio.Reader但由于fasthttp已經將數據讀入[]byte,因此更適合使用json.Unmarshal

   err := json.Unmarshal(data, &auctions)

    if err != nil {

         return nil, err

    }

不要忘記處理來自json.Unmarshal(或,json.Decoder.Decode就此而言)的錯誤。 acutions如果 Json 無法解析,則不會保存預期的數據,因此您應該處理這種可能性。


查看完整回答
反對 回復 2022-10-17
  • 1 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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