英文:
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
}
请注意,我只翻译了代码部分,其他内容不包括在内。
英文:
So again im trying to get this data but it is returning an error of
data.Body undefined (type []byte has no field or method Body)
on line 16 and 23 of this code. so when its decoding the json
If anyone could help me,
here is my code
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
得分: 2
json.NewDecoder(data.Body).Decode(&auctions)
> > data.Body undefined (type []byte has no field or method Body) >
json.NewDecoder
期望一个io.Reader
,但由于fasthttp
已经将数据读入[]byte
中,更合适的做法是使用json.Unmarshal
:
err := json.Unmarshal(data, &auctions)
if err != nil {
return nil, err
}
不要忘记处理json.Unmarshal
的错误(或者同样地,处理json.Decoder.Decode
的错误)。如果Json解析失败,acutions
将不会保存预期的数据,所以你应该处理这种可能性。
英文:
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
}
Don't forget to handle errors from json.Unmarshal
(or, from json.Decoder.Decode
for that matter). acutions
won't hold the expected data if the Json failed to parse, so you should handle that possiblity.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论