繁体   English   中英

在Go中创建JSON或slice数组

[英]Create a JSON or slice array in Go

我不知道该怎么做或说这个。 我有一个变量返回类似这样的字符串:

unixCommands := exec.Command("ls", "/bin")
unixCommandsout, err := unixCommands.Output()
unixCommandsstring := string(unixCommandsout)
fmt.Printf(unixCommandsstring)

输出:

unicode_start
unicode_stop
unlink
usleep
vi
view
ypdomainname
zcat

我正在寻找创建JSON数组或最简单的方法来获得此最终输出:

["unicode_start", "unicode_stop", "unlink", "usleep", "vi", "view", "ypdomainname", "zcat"]

您可以使用包encoding/json做到这一点:

outputSlice := strings.Split(unixCommandsstring,"\n")
js,_ := json.Marshal(outputSlice)
fmt.Print(string(js))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM