我有这条数据:
productID, err := products.Insert(map[string]interface{}{
"Properties": map[string]interface{}{
strconv.Itoa(propertyNameID): map[string]string{
"en": "Jeans Jersey",
"nl": "Broek Jersey",
},
strconv.Itoa(propertyColorID): propertyOptionRedID,
},
"Type": productTypeID,
"Propertyset": propertysetID,
"Active": true,
"EAN13": "1234567890123"})
所有***ID
变量都是int
类型。可悲的是,当我做一个普通的编码(marshal)时:
{
"Active":true,
"EAN13":"1234567890123",
"Properties":{
"2286408386526632249":{
"en":"Jeans Jersey",
"nl":"Broek Jersey"
},
"4750062295175300168":7.908474319828591e+18
},
"Propertyset":8.882218269088507e+18,
"Type":7.185126253999425e+18
}
...一些int被转换为float
类型的幂。
Itoa
仍然只是一些测试,因为编码器无法执行 map[int]interface{}
(索引值作为整数的列表)。我只是不明白为什么 int
值会更改为它们的“显示”值,而不是它们的纯值。
更新:我尝试使用 map[string]int
进行“属性”操作,并且只有一个条目。结果还是一样:(
最佳答案
您可以使用 string
标记将 int64 编码为 json 中的字符串,以避免转换为 float64
type T struct {
Val int64 `json:"val,string"`
}
t := T{Val: math.MaxInt64}
j, _ := json.Marshal(t)
fmt.Println(string(j))
// {"val":"9223372036854775807"}
关于json - 将 JSON 大整数编码为 float 的幂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29241443/