我有一個以老式方式轉換為 JSON 的結構:type Output struct { Name string `json:"name"` Command string `json:"command"` Status int `json:"status"` Output string `json:"output"` Ttl int `json:"ttl,omitempty"` Source string `json:"source,omitempty"` Handlers []string `json:"handlers,omitempty"` }sensu_values := &Output{ Name: name, Command: command, Status: status, Output: output, Ttl: ttl, Source: source, Handlers: [handlers], }我想從文件系統(tǒng)中讀取任意 JSON 文件,用戶可以將其定義為任何內容,然后將其添加到現(xiàn)有 JSON 字符串中,并從原始文件中獲取副本。我怎樣才能做到這一點?
1 回答

富國滬深
TA貢獻1790條經驗 獲得超9個贊
輸入 JSON :
{
"environment": "production",
"runbook": "http://url",
"message": "there is a problem"
}
最好在編組結構之前解組輸入 JSON 并組合這兩個結構Output。
示例代碼
inputJSON := `{"environment": "production", "runbook":"http://url","message":"there is a problem"}`
out := map[string]interface{}{}
json.Unmarshal([]byte(inputJSON), &out)
out["name"] = sensu_values.Name
out["command"] = sensu_values.Command
out["status"] = sensu_values.Status
outputJSON, _ := json.Marshal(out)
- 1 回答
- 0 關注
- 339 瀏覽
添加回答
舉報
0/150
提交
取消