[英]In Go-Lang how can i take JSON file and quote to css
I have sumfile.json that needs to be quoted into specific format.我有 sumfile.json 需要引用为特定格式。
{
"address": "312312321321",
"number": "d56"
"type": "chocolate",
"model": "dcdas55A",
"partnumber": "adasdasA",
...
and i have to make it look like this:我必须让它看起来像这样:
"data": "{\"address\": null, \"number\": \"-DI1\", \"_type\": \"\", \"model\": \"CI STO\", \"number\": \"603sjhhd2\",
Just read the file into a byte slice, convert the bytes into a string, and then marshal the string as JSON.只需将文件读入字节切片,将字节转换为字符串,然后将字符串编组为 JSON。
f, err := os.Open("sumfile.json")
if err != nil {
panic(err)
}
defer f.Close()
raw, err := io.ReadAll(f)
if err != nil {
panic(err)
}
out, err := json.Marshal(map[string]string{"data": string(raw)})
if err != nil {
panic(err)
}
fmt.Println(string(out))
https://go.dev/play/p/0lCwHLC8Tno https://go.dev/play/p/0lCwHLC8Tno
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.