【发布时间】:2021-12-21 22:20:05
【问题描述】:
我正在公开一个带有一些数据的 REST 端点。这是一个结构,比如说:
type status struct {
Config struct {
Allow bool `json:"allow"`
Expired bool `json:"expired"`
}
Database struct {
Healthy bool `json:"healthy"`
WaitCount int64 `json:"wait_count"`
}
}
我使用 json 标记来表示调用端点时结构字段的外观。使用上述内容,我得到以下有效负载作为响应:
{
"Config": {
"allow": false,
"expired": false,
},
"Database": {
"healthy": true,
"wait_count": 1,
},
}
我希望 Config 和 Database 小写,意思是 config 和 database。但是,将它们更改为 Go 代码中的内容意味着 "encoding/json" 包无法“看到”它们,因为它们没有导出到包范围之外。
如何在 json 响应负载中小写嵌套结构?
【问题讨论】: