【发布时间】:2014-02-04 18:26:49
【问题描述】:
我有结构
type tySurvey struct {
Id int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
我确实 json.Marshal 在 HTML 页面中写入 JSON 字节。 jQuery 修改对象中的 name 字段并使用 jQueries JSON.stringify 和 jQuery 将字符串发送到 Go 处理程序对对象进行编码。
id 字段编码为字符串。
发送:{"id":1} 接收:{"id":"1"}
问题是 json.Unmarshal 无法解组该 JSON,因为 id 不再是整数。
json: cannot unmarshal string into Go value of type int64
处理此类数据的最佳方法是什么?我不想手动转换每个字段。我希望编写紧凑、无错误的代码。
行情还不错。 JavaScript 不适用于 int64。
我想学习用 int64 值中的字符串值解组 json 的简单方法。
【问题讨论】:
-
有没有办法知道是哪个字段导致了问题?
标签: json go marshalling unmarshalling