json - 将 JSON 大整数编码为 float 的幂

标签 json types go marshalling

我有这条数据:

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/

相关文章:

c - 为什么指针声明中的星号特定于标识符而不是数据类型?

Go 模板不会显示结构 slice

string - golang 中的 100 阶乘

json - 在维基共享资源中按文件名查找图像

postgresql - 为什么 'now()'::timestamp 返回 PostgreSQL 中的当前时间戳?

c++ - 在 C++ 中捕获类型错误

go - '*' 和 '&' 是什么意思?

java - 使用 Gson 将内部 ArrayList<POJO> 转换为 JSON

json - Spring MVC 中的 Jackson Object Mapper 无法正常工作

javascript - 将数据从 ajax success 传递到外部 else/if 语句