error "data1.Body undefined (type []byte has no field or method Body)" when trying to decode a json's body

huangapple go评论143阅读模式
英文:

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, &quot;https://api.hypixel.net/skyblock/auctions&quot;)
	if err != nil {
		return auctions, err
	}
	fmt.Println(statusCode)
	var totalPages = auctions.TotalAuctions
	for i := 0; i &lt; totalPages; i++ {
		statusCode, data1, err := fasthttp.Get(nil, &quot;https://api.hypixel.net/skyblock/auctions&quot;)
		if err != nil {
			return auctions, err
		}
		fmt.Println(statusCode)
		json.NewDecoder(data1.Body).Decode(&amp;auctions)
		fmt.Println(auctions.LastUpdated)
	}
	endTime := time.Now()
	var timeTook = endTime.Sub(startTime).Milliseconds()
	fmt.Println(data)

	json.NewDecoder(data.Body).Decode(&amp;auctions)

	fmt.Println(auctions.LastUpdated)
	fmt.Println(timeTook)

	return auctions, err
}

答案1

得分: 2

json.NewDecoder(data.Body).Decode(&auctions)

> &gt; data.Body undefined (type []byte has no field or method Body) &gt;

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的错误)。如果Json解析失败,acutions将不会保存预期的数据,所以你应该处理这种可能性。

英文:
    json.NewDecoder(data.Body).Decode(&amp;auctions)

>
&gt; data.Body undefined (type []byte has no field or method Body)
&gt;

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, &amp;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.

huangapple
  • 本文由 发表于 2021年12月4日 03:04:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/70219507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定