我有以下结构要导出为 json:
type ExportedIncident struct {
Title string `json:"title"`
Host string `json:"host"`
Status string `json:"status"`
Date string `json:"date"`
Notes []ExportedNote `json:"notes"`
LogEntries []ExportedLogEntry `json:"log_entries"`
}
我想要下划线大小写字段,所以我必须为此定义每个字段,如本答案中所述:https://stackoverflow.com/a/11694255/1731473 https://stackoverflow.com/a/11694255/1731473
但这确实很麻烦,我相信Go中有一个更简单的解决方案,但我找不到它。
如何为 JSON 导出设置默认字母大小写(下划线、蛇形、驼峰...)?
不幸的是,没有机会将您的字段导出到snake_case
所以你必须自己维护标签。
从技术上讲,你可以使用方法MarshalJSON
并在该方法内执行所有操作,但这并不是更简单的方法......
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)