【发布时间】:2021-10-18 13:10:10
【问题描述】:
我调用第三方 API 并得到以下 JSON:
{"amount":1.0282E+7}
当我想转换它时出现错误:
Blocjson:无法将数字 1.0282E+7 解组到 Go 结构字段 MiddleEastAccountToCardResponse.amount 类型的 int64
我想将此 JSON 转换为 Go 中的以下结构:
type Response struct {
Amount int64 `json:"amount"`
}
【问题讨论】:
-
1.0282E+7那是 float64 -
这能回答你的问题吗? Golang Json Unmarshal numeric with exponent
-
@nipuna :如果 OP 需要 struct 字段的类型为 int64 (我的意思是如果重新定义为 float64 不是一个选项),我真的没有看到我在问题中解决了这个问题链接。可以通过编写自定义Unmarshaller 或我猜的别名类型来解决。
标签: json go marshalling