我有以下结构可以导出到 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
但这真的很麻烦,我相信 Go 中有更简单的解决方案,但我找不到。
如何为 JSON 导出设置默认字母大小写(下划线、蛇形、驼峰...)?
最佳答案
不幸的是,没有机会将您的字段导出到 snake_case
中,因此您必须自己维护标签。
从技术上讲,您可以使用方法 MarshalJSON
并在此方法内执行所有操作,但这并不是更简单的方法...
关于json - go json marshal 的默认大小写选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55258546/