【发布时间】:2021-11-26 00:52:02
【问题描述】:
如何在此代码的编组 JSON 输出中将密钥名称 Id 设为小写?
type Topic struct {
Id string
}
topics := []Topic{
{Id: "some identifier"},
{Id: "some other identifier"},
}
tops, err := json.Marshal(topics)
if err != nil {
fmt.Println("got an error", err)
}
fmt.Println(string(tops))
返回:
[
{"Id":"some identifier"},
{"Id":"some other identifier"}
]
但我使用的 API 需要小写,例如:
[
{"id":"some identifier"},
{"id":"some other identifier"}
]
我对 golang 还是很陌生,所以任何方向都值得赞赏!
【问题讨论】: