我有一个结构,我以老式的方式转换为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],
}
Run Code Online (Sandbox Code Playgroud)
我想从文件系统中读取任意JSON文件,该文件可以由用户定义为任何内容,然后将其添加到现有的JSON字符串中,从原始文件中获取重复项.
我怎样才能做到这一点?
输入JSON:
{
"environment": "production",
"runbook": "http://url",
"message": "there is a problem"
}
Run Code Online (Sandbox Code Playgroud)
在编组结构之前,最好解组输入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)
Run Code Online (Sandbox Code Playgroud)