我正在公開一個帶有一些資料的 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,
},
}
我希望 forConfig和Database是小寫,意思是configand database。但是,將它們更改為 Go 代碼中的值意味著"encoding/json"包無法“看到”它們,因為它們沒有匯出到包范圍之外。
如何小寫 json 回應負載中的嵌套結構?
uj5u.com熱心網友回復:
嵌套結構是包含結構中的一個欄位。像處理其他欄位一樣添加欄位標簽:
type status struct {
Config struct {
Allow bool `json:"allow"`
Expired bool `json:"expired"`
} `json:"config"` // <-- add tag here ...
Database struct {
Healthy bool `json:"healthy"`
WaitCount int64 `json:"wait_count"`
} `json:"database"` // <-- ... and here
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/353825.html
上一篇:Thingsboard:在一個SaveTimeseriesNodePost中保存多個時間序列讀數
下一篇:Go無法將func識別為有效介面
