json库精度丢失问题
当用enconding/json
包的时候,数字默认是处理为float64类型的,这就导致了int64可能会丢失精度
举个例子
1 | func err() { |
本来json里面的id是1153273393983037404,转成interface{}
后会变成float64
类型,再转回json的时候精度丢失就发生了
解决方案
直接用string存,然后自行用
strconv
转换使用
json.decoder
来替换Unmarshal
,其实底层也是用string
存,不过是封装好了的。。,上个实例1
2
3
4
5
6
7
8
9
10
11
12
13
14func right() {
s := "{\"id\": 1153273393983037404}"
mm := make(map[string]interface{})
decoder := json.NewDecoder(bytes.NewReader([]byte(s)))
decoder.UseNumber()
_ = decoder.Decode(&mm)
fmt.Println(mm)
jstr, _ := json.Marshal(mm)
fmt.Println(string(jstr))
}
###
map[id:1153273393983037404]
{"id":1153273393983037404}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 SHIELD!
评论