所以我再次嘗試獲取這些資料,但它回傳了一個錯誤
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
}
uj5u.com熱心網友回復:
json.NewDecoder(data.Body).Decode(&auctions)
data.Body undefined (type []byte has no field or method Body)
data已經是回應體了。
json.NewDecoder期望 ,io.Reader但由于fasthttp已經將資料讀入[]byte,因此使用更合適json.Unmarshal:
err := json.Unmarshal(data, &auctions)
if err != nil {
return nil, err
}
不要忘記處理來自json.Unmarshal(或,來自json.Decoder.Decode)的錯誤。 acutions如果 Json 決議失敗,則不會保存預期的資料,因此您應該處理這種可能性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/373114.html
