[英]preserve int64 values when parsing json in Go
我正在 Go 中处理一个 json POST,其中包含一个包含 64 位整数的对象数组。 使用 json.Unmarshal 时,这些值似乎被转换为 float64,这不是很有帮助。
body := []byte(`{"tags":[{"id":4418489049307132905},{"id":4418489049307132906}]}`)
var dat map[string]interface{}
if err := json.Unmarshal(body, &dat); err != nil {
panic(err)
}
tags := dat["tags"].([]interface{})
for i, tag := range tags {
fmt.Println("tag: ", i, " id: ", tag.(map[string]interface{})["id"].(int64))
}
有没有办法在 json.Unmarshal 的输出中保留原始的 int64?
解决方案1
您可以使用Decoder和UseNumber来无损地解码您的数字:
Number
类型定义如下:
// A Number represents a JSON number literal.
type Number string
这意味着您可以轻松转换它:
package main
import (
"encoding/json"
"fmt"
"bytes"
"strconv"
)
func main() {
body := []byte("{\"tags\":[{\"id\":4418489049307132905},{\"id\":4418489049307132906}]}")
dat := make(map[string]interface{})
d := json.NewDecoder(bytes.NewBuffer(body))
d.UseNumber()
if err := d.Decode(&dat); err != nil {
panic(err)
}
tags := dat["tags"].([]interface{})
n := tags[0].(map[string]interface{})["id"].(json.Number)
i64, _ := strconv.ParseUint(string(n), 10, 64)
fmt.Println(i64) // prints 4418489049307132905
}
解决方案2
您还可以解码为适合您需求的特定结构:
package main
import (
"encoding/json"
"fmt"
)
type A struct {
Tags []map[string]uint64 // "tags"
}
func main() {
body := []byte("{\"tags\":[{\"id\":4418489049307132905},{\"id\":4418489049307132906}]}")
var a A
if err := json.Unmarshal(body, &a); err != nil {
panic(err)
}
fmt.Println(a.Tags[0]["id"]) // logs 4418489049307132905
}
就个人而言,我通常更喜欢这种感觉更有条理且更易于维护的解决方案。
警告
如果您使用 JSON,请注意一点,因为您的应用程序部分使用 JavaScript:JavaScript 没有 64 位整数,而只有一种数字类型,即 IEEE754 双精度浮点数。 因此,您将无法使用标准解析函数在 JavaScript 中解析此 JSON 而不丢失。
更简单的一个:
body := []byte(`{"tags":[{"id":4418489049307132905},{"id":4418489049307132906}]}`)
var dat map[string]interface{}
if err := json.Unmarshal(body, &dat); err != nil {
panic(err)
}
tags := dat["tags"].([]interface{})
for i, tag := range tags {
fmt.Printf("tag: %v, id: %.0f", i, tag.(map[string]interface{})["id"].(float64))
}
我意识到这很旧,但这是我最终使用的解决方案
/*
skipping previous code, this is just converting the float
to an int, if the value is the same with or without what's
after the decimal points
*/
f := tag.(map[string]interface{})["id"].(float64)
if math.Floor(f) == f {
fmt.Println("int tag: ", i, " id: ", int64(f))
} else {
fmt.Println("tag: ", i, " id: ", f)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.