我不明白如何比較未編組的 JSON。例子:package mainimport ( "fmt" "reflect" "encoding/json")func main() { a := map[string]interface{} {"foo": 1, "bar": 2} b := map[string]interface{} {"bar": 2, "foo": 1} fmt.Printf("Literal B is %v, DeepEqual is %v\n", b, reflect.DeepEqual(a, b)) err := json.Unmarshal([]byte(`{"bar": 2, "foo": 1}`), &b) if err != nil { panic("Could not unmarshal") } fmt.Printf("Umarshalled B is %v, DeepEqual is %v\n", b, reflect.DeepEqual(a, b))}印刷文字 B 是 map[bar:2 foo:1],DeepEqual 為真Umarshalled B 是 map[bar:2 foo:1],DeepEqual 是假的在 JSON 解組之后,從文字初始化的 B 和 B 有什么不同?
1 回答

胡子哥哥
TA貢獻1825條經(jīng)驗 獲得超6個贊
這回答了你的問題:
fmt.Printf("%T\n", b["foo"]) // Prints "float64" after unmarshaling.
JSON 數(shù)字是 64 位浮點數(shù),而您的原始地圖值是整數(shù)。從文檔:
要將 JSON 解組為接口值,Unmarshal 將其中之一存儲在接口值中:
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
- 1 回答
- 0 關注
- 160 瀏覽
添加回答
舉報
0/150
提交
取消