英文:
Adding format characters such as \t and \n to a string of JSON so they will work in a linux echo command
问题
我有一个到Linux机器的开放SSH连接。我正在创建一个临时文件,并且需要使用echo命令将格式化的JSON写入其中。有没有办法可以轻松地在必要的地方添加\t和\n,以便最终在临时文件中呈现格式化的JSON?在我尝试创建一个解析函数并手动添加\t和\n之前,有没有其他方法?
jsonData := "{\"data\":[{\"id\": \"1000\", \"name\":\"Product 1\"}, {\"id\": \"2000\", \"name\":\"Product 2\"}, {\"id\": \"3000\", \"name\":\"Product 3\"}]}"
// 创建一个临时文件
session1, err := conn.NewSession()
if err != nil {
panic("Failed to create session: " + err.Error())
}
defer session1.Close()
var tmpFileName bytes.Buffer
session1.Stdout = &tmpFileName
if err := session1.Run("mktemp"); err != nil {
panic("Failed to run: " + err.Error())
}
// 将JSON写入临时文件
session2, err := conn.NewSession()
if err != nil {
panic("Failed to create session: " + err.Error())
}
defer session2.Close()
if err := session2.Run("echo " + jsonData + ">> " + tmpFileName.String()); err != nil {
panic("Failed to run: " + err.Error())
}
英文:
I have an open ssh connection to a linux machine. I am creating a tempfile and need to write formatted Json to it using the echo command. Is there anyway I can easily add the \t and \n where necessary so it ends up being formatted json in the tempfile? Before I resort to creating a function that parses through it and manually adds the \t and \n.
jsonData := "{\"data\":[{\"id\": \"1000\", \"name\":\"Product 1\"}, {\"id\": \"2000\", \"name\":\"Product 2\"}, {\"id\": \"3000\", \"name\":\"Product 3\"}]}"
// Create a temp file
session1, err := conn.NewSession()
if err != nil {
panic("Failed to create session: " + err.Error())
}
defer session1.Close()
var tmpFileName bytes.Buffer
session1.Stdout = &tmpFileName
if err := session1.Run("mktemp"); err != nil {
panic("Failed to run: " + err.Error())
}
// Write json to temp file
session2, err := conn.NewSession()
if err != nil {
panic("Failed to create session: " + err.Error())
}
defer session2.Close()
if err := session2.Run("echo " + jsonData + ">> " + tmpFileName.String()); err != nil {
panic("Failed to run: " + err.Error())
}
答案1
得分: 1
你可以使用json.MarshalIndent
函数来实现:
out, err := json.MarshalIndent(json.RawMessage(jsonData), "", " ")
if err != nil {
panic(err)
}
cmd := exec.Command("echo", string(out))
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
panic(err)
}
根据session2.Run
的实现方式不同,你可能需要对格式化后的JSON字符串进行转义并显式地加上引号。
// 这是一个简单的单引号转义方法,特定于bash,如果对你的用例不够适用,你需要进行改进。
jsonData = strings.ReplaceAll(string(out), "'", `'"'"'`)
cmd := fmt.Sprintf("echo '%s' >> %s", jsonData, tmpFileName)
if err := session2.Run(cmd); err != nil {
panic(err)
}
英文:
You can use json.MarshalIndent
:
out, err := json.MarshalIndent(json.RawMessage(jsonData), "", " ")
if err != nil {
panic(err)
}
cmd := exec.Command("echo", string(out))
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
panic(err)
}
https://play.golang.org/p/J4Z5txNOmgP
Depending on how session2.Run
is implemented you may need to escape and explicitly quote the formatted json string.
// this is a naive single quote bash-specific escape, if this isn't
// sufficient for your use case you'll have to improve on it.
jsonData = strings.ReplaceAll(string(out), "'", `'"'"'`)
cmd := fmt.Sprintf("echo '%s' >> %s", jsonData, tmpFileName)
if err := session2.Run(cmd); err != nil {
panic(err)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论