Go地图到JSON的列表

huangapple go评论121阅读模式
英文:

List of Go maps to Json

问题

你可以使用json.NewEncoder来实现你想要的 JSON 文件格式。这个方法可以让你更加灵活地控制 JSON 的生成过程。下面是一个示例代码,展示了如何使用json.NewEncoder来生成你期望的 JSON 文件格式:

  1. package main
  2. import (
  3. "encoding/json"
  4. "os"
  5. )
  6. func main() {
  7. m := []map[string]interface{}{
  8. {"a1": "aa1", "b1": "bb1"},
  9. {"a2": "aa2", "b2": "bb2"},
  10. {"a3": "aa3", "b3": "bb3"},
  11. }
  12. file, err := os.Create("file.json")
  13. if err != nil {
  14. panic(err)
  15. }
  16. defer file.Close()
  17. encoder := json.NewEncoder(file)
  18. encoder.SetIndent("", "\t")
  19. for _, item := range m {
  20. err := encoder.Encode(item)
  21. if err != nil {
  22. panic(err)
  23. }
  24. }
  25. }

这段代码会生成一个名为file.json的文件,其中的内容符合你期望的格式。在这个格式中,每个 map 都会单独占据一行,没有[]符号,也没有逗号分隔符。同时,每行的开头也没有空格缩进。

你可以根据需要修改代码中的变量名和文件名。希望这可以帮助到你!

英文:

I want to parse a list of maps in Go in a json file in a specific format. Following is the list that I have:

  1. m := []map[string]interface{}{}
  2. k1 := map[string]interface{}{"a1": "aa1", "b1": "bb1"}
  3. k2 := map[string]interface{}{"a2": "aa2", "b2": "bb2"}
  4. k3 := map[string]interface{}{"a3": "aa3", "b3": "bb3"}
  5. m = append(m, k1, k2, k3)

This is how I parse it to a json file.

  1. jsonFile, _ := json.MarshalIndent(m, "", "\t")
  2. ioutil.WriteFile("file.json", jsonFile, os.ModePerm)

In the json file, I want:

  • there to be no [ or ] symbols at the beginning or end.
  • Each map to be in a new line
  • there to be no comma between successive maps
  • no space indentation at start of line.

This is how my json file looks at present:

  1. [
  2. {
  3. "a1": "aa1",
  4. "b1": "bb1"
  5. },
  6. {
  7. "a2": "aa2",
  8. "b2": "bb2"
  9. },
  10. {
  11. "a3": "aa3",
  12. "b3": "bb3"
  13. }
  14. ]

Below is how I want the output in my saved json file to look:

  1. {
  2. "a1": "aa1",
  3. "b1": "bb1"
  4. }
  5. {
  6. "a2": "aa2",
  7. "b2": "bb2"
  8. }
  9. {
  10. "a3": "aa3",
  11. "b3": "bb3"
  12. }

I realize that I am able to have every map in a new line. So that is done. But, removal of [ or ] symbols, commas after successive maps and indentation is yet to be done. How can I do this?

答案1

得分: 2

您的期望输出是一系列独立的JSON对象。使用json.Encoder来逐个编码这些对象。

可以像这样实现:

  1. enc := json.NewEncoder(os.Stdout)
  2. enc.SetIndent("", " ")
  3. for _, v := range m {
  4. if err := enc.Encode(v); err != nil {
  5. panic(err)
  6. }
  7. }

这将输出以下内容(在Go Playground上尝试):

  1. {
  2. "a1": "aa1",
  3. "b1": "bb1"
  4. }
  5. {
  6. "a2": "aa2",
  7. "b2": "bb2"
  8. }
  9. {
  10. "a3": "aa3",
  11. "b3": "bb3"
  12. }

此示例将JSON文本写入标准输出。如果要写入文件,请将os.Stdout替换为*os.File类型的文件值。

英文:

Your desired output is a series of independent JSON objects. Use a json.Encoder to encode the objects individually.

Something like this:

  1. enc := json.NewEncoder(os.Stdout)
  2. enc.SetIndent("", " ")
  3. for _, v := range m {
  4. if err := enc.Encode(v); err != nil {
  5. panic(err)
  6. }
  7. }

This will output (try it on the Go Playground):

  1. {
  2. "a1": "aa1",
  3. "b1": "bb1"
  4. }
  5. {
  6. "a2": "aa2",
  7. "b2": "bb2"
  8. }
  9. {
  10. "a3": "aa3",
  11. "b3": "bb3"
  12. }

This example writes the JSON text to the standard output. To log to a file, obviously pass an *os.File value instead of os.Stdout.

huangapple
  • 本文由 发表于 2021年12月4日 05:28:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/70220867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定