Other ways of decoding numbers
When decoded into an interface{}
, JSON numbers are converted to float64
. This is not always the desired result. You can use json.Number
instead.
How to do it...
Use json.Decoder
with UseNumber
:
var output interface{} decoder:=json.NewDecoder(strings.NewReader(`[1.1,2,3,4.4]`)) // Tell the decoder to use json.Number instead of float64 decoder.UseNumber() err:=decoder.Decode(&output) // [1.1 2 3 4.4]
Every element of output
in the preceding example is an instance of json.Number
. You can translate it to an int
, float64
, or big.Int
as necessary.