我正在嘗試創建 JSON 以發送到接受以下格式的 Rails API:{"device":{"ipaddress":"192.168.1.2", "netmask": "255.255.255.0", "gateway": "192.168.1.1"}}但我不確定如何將我已經編組的內容包裝到"device":{}API 接受的部分中。這是我到目前為止所擁有的:type Device struct { IPAddress string `json:"ipaddress"` Network string `json:"network"` Gateway string `json:"gateway"`}// gathers the IP infofunc GatherIPInfo() { d := Device{ IPAddress: "192.168.1.2", Network: "255.255.255.0", Gateway: "192.168.1.1", } // now send to API data, _ := json.Marshal(d) // looks like: {"ipaddress":"192.168.1.2","network":"255.255.255.0","gateway":"192.168.1.1"} ... ...}我是否必須創建另一個結構才能將現有結構包裝到“設備”中,或者是否有更簡單的方法?謝謝!
將 Go 結構包裝并編組到所需的 JSON 參數中
慕蓋茨4494581
2023-07-17 15:17:09