尝试解码 json 的正文时出现错误 "data1.Body undefined (type []byte has no field or method Body)"
error "data1.Body undefined (type []byte has no field or method Body)" when trying to decode a json's body
所以我再次尝试获取此数据,但它返回了
的错误
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
}
json.NewDecoder(data.Body).Decode(&auctions)
data.Body undefined (type []byte has no field or method Body)
data
is already the body of the response.
json.NewDecoder
expects an io.Reader
but since fasthttp
has already read the data into []byte
, it would be more appropriate to use json.Unmarshal
:
err := json.Unmarshal(data, &auctions)
if err != nil {
return nil, err
}
不要忘记处理来自 json.Unmarshal
的错误(或者,来自 json.Decoder.Decode
的错误)。如果 Json 解析失败,acutions
将不会保留预期的数据,因此您应该处理这种可能性。
所以我再次尝试获取此数据,但它返回了
的错误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
}
json.NewDecoder(data.Body).Decode(&auctions)
data.Body undefined (type []byte has no field or method Body)
data
is already the body of the response.
json.NewDecoder
expects an io.Reader
but since fasthttp
has already read the data into []byte
, it would be more appropriate to use json.Unmarshal
:
err := json.Unmarshal(data, &auctions)
if err != nil {
return nil, err
}
不要忘记处理来自 json.Unmarshal
的错误(或者,来自 json.Decoder.Decode
的错误)。如果 Json 解析失败,acutions
将不会保留预期的数据,因此您应该处理这种可能性。