最近发现 golang解析json时把所有的int,float,double等数字,向interface{}解析时都当成float64(当然被双信号包围的数字除外,任何被双引号包围的,都是字符串。),不知道是golang的内部约定还是啥。
package main
import (
"encoding/json"
"fmt"
)
func main() {
var v map[string]interface{}
jsonstr := `{"id":13,"name":"胖胖","weight":216.5,"dd":"123"}`
json.Unmarshal([]byte(jsonstr), &v)
for k, v1 := range v {
fmt.Print(k, " = ")
switch v1.(type) {
case int:
fmt.Println(v1, "is an int value.")
case string:
fmt.Println(v1, "is a string value.")
case int64:
fmt.Println(v1, "is an int64 value.")
case float64:
fmt.Println(v1, "is an float64 value.")
default:
fmt.Println(v1, "is an unknown type.")
}
}
}
输出如下:
API server listening at: 127.0.0.1:36104
weight = 216.5 is an float64 value.
dd = 123 is a string value.
id = 13 is an float64 value.
name = 胖胖 is a string value.
Process exiting with code: 0
仅作记录。
有疑问加站长微信联系(非本文作者)