所以我再次嘗試獲取這些數據,但它返回一個錯誤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)
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 無法解析,則不會保存預期的數據,因此您應該處理這種可能性。
- 1 回答
- 0 關注
- 148 瀏覽
添加回答
舉報
0/150
提交
取消