因此,我再次尝试获取该数据,但它返回的错误为
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
}发布于 2021-12-03 19:17:17
json.NewDecoder(data.Body).Decode(&auctions)data.Body未定义(类型[]字节没有字段或方法主体)
data is already the body of the response。
json.NewDecoder expects an io.Reader,但是由于fasthttp已经将数据读入[]byte,所以使用json.Unmarshal更合适。
err := json.Unmarshal(data, &auctions)
if err != nil {
return nil, err
}不要忘记处理来自json.Unmarshal (或者json.Decoder.Decode )的错误。如果Json不能解析,acutions将不会保存预期的数据,因此您应该处理这种可能性。
https://stackoverflow.com/questions/70219507
复制相似问题