我有结构
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的简单方法。
这可以通过添加,string
到标签中来进行处理,如下所示:
type tySurvey struct {
Id int64 `json:"id,string,omitempty"`
Name string `json:"name,omitempty"`
}
这可以在Marshal的文档中找到。
请注意,您不能通过指定来解码空字符串,omitempty
因为它仅在编码时使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句