简体   繁体   English

在 Go-Lang 中,我如何获取 JSON 文件并引用 css

[英]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.

相关问题 如何直接从 Go 中的 GCP 服务帐户 JSON 密钥文件创建 kube.netes.Clientset? - How can I create a kubernetes.Clientset directly from a GCP service account JSON key file in Go? GCP:如何备份安装到文件存储的实例,或者基本上我想备份谷歌云平台中的文件存储 - GCP: How can I take backup of an instance that is mount to the file store or basically I want to take a backup of file store in google cloud platform 如何使用 JSON 服务帐户密钥在 GO 中获取 Kubernetes 客户端集? - How can I get a Kubernetes clientset in GO using a JSON service account key? 如何在 Python 中直接在 s3 中的文件中写入 JSON? - How can I write JSON in file in s3 directly in Python? 如何将 JSON 文件上传到 Google Cloud Storage? - How can I upload JSON file to Google Cloud Storage? 如何在数组中写入 map 并将其添加到 go 中的 json 文件中? - How to write a map inside a array and adding that to a json file in go? GO:我如何使用 go 反射来设置结构指针的值 - GO: how can i use go reflect to set the value of pointer of struct 如何在 Go[lang] 中将字节转换为字符串 - How To Convert Bytes Into Strings In Go[lang] 在 Go 中将文件放入 amazon s3 后,如何获取文件 url? - How do I get file url after put file to amazon s3 in Go? 我如何在 Lambda python function 中将 dynamodb json 转换为 json - How can i convert dynamodb json to json in a Lambda python function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM