I am trying out json.Decoder and got something strange happening. The same code will NOT produce consistent result. Sometimes it works fine; sometimes it errors: not at beginning of value from calling Decode.
func main() {
simpleRequest()
}
type FxPair struct {
CurrencyCodeA int `json:"currencyCodeA"`
CurrencyCodeB int `json:"currencyCodeB"`
Date int `json:"date"`
RateBuy float64 `json:"rateBuy"`
RateSell float64 `json:"rateSell"`
RateCross float64 `json:"rateCross"`
}
func simpleRequest() {
res, err := http.Get("https://api.monobank.ua/bank/currency")
if err != nil {
panic(err)
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
_, err = decoder.Token() // read the opening square bracket
if err != nil {
panic(err)
}
var pair FxPair
for decoder.More() {
err := decoder.Decode(&pair)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", pair) // process a pair
}
_, err = decoder.Token() // read the closing square bracket
if err != nil {
panic(err)
}
}