Golang JSON 不同类型反射的数组:float64 与 int64

Golang JSON array of different types reflection: float64 vs int64

考虑这个简单的例子:

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "reflect"
)

var args = `[1, 2.5, "aaa", true, false]`

func main() {
    var x []interface{}
    err := json.Unmarshal([]byte(args), &x)

    if err != nil {
        log.Fatalf("%s", err.Error())
        panic(fmt.Sprintf("%s", err.Error()))
    }

    for _, arg := range x {
        t := reflect.TypeOf(arg).Kind().String()
        v := reflect.ValueOf(arg)

        if t == "int64" {
            fmt.Printf("int64 %v\n", v.Int())
        }

        if t == "float64" {
            fmt.Printf("float64 %v\n", v.Float())
        }

        if t == "string" {
            fmt.Printf("string %v\n", v.String())
        }

        if t == "bool" {
            fmt.Printf("bool %v\n", v.Bool())
        }
    }
}

程序输出:

float64 1
float64 2.5
string aaa
bool true
bool false

如您所见,我的输入是一个有效的 JSON,它代表一个包含五个项目的数组:

- integer
- floating point number
- string
- boolean
- boolean

当我将有效的 JSON 字符串解组为 []interface{} 并尝试使用反射检查类型时,JSON 中的整数值具有 float64 类型。知道为什么吗?这是预期的行为吗?

这是 Unmarshal 的记录行为。所有数字都解组为 float64。

To unmarshal JSON into an interface value, Unmarshal stores one of these in the interface value:

  • bool, for JSON booleans
  • float64, for JSON numbers
  • string, for JSON strings
  • []interface{}, for JSON arrays
  • map[string]interface{}, for JSON objects
  • nil for JSON null

这是因为JSON没有整数,JSON中的每个数字都定义为64位浮点数。

这是 JSON 解码器的默认行为。你可以改成输出json.Number instead by using the UseNumber的方法。