Go地图到JSON的列表

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

List of Go maps to Json

问题

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

package main

import (
	"encoding/json"
	"os"
)

func main() {
	m := []map[string]interface{}{
		{"a1": "aa1", "b1": "bb1"},
		{"a2": "aa2", "b2": "bb2"},
		{"a3": "aa3", "b3": "bb3"},
	}

	file, err := os.Create("file.json")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	encoder := json.NewEncoder(file)
	encoder.SetIndent("", "\t")

	for _, item := range m {
		err := encoder.Encode(item)
		if err != nil {
			panic(err)
		}
	}
}

这段代码会生成一个名为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:

m := []map[string]interface{}{}

k1 := map[string]interface{}{"a1": "aa1", "b1": "bb1"}
k2 := map[string]interface{}{"a2": "aa2", "b2": "bb2"}
k3 := map[string]interface{}{"a3": "aa3", "b3": "bb3"}

m = append(m, k1, k2, k3)

This is how I parse it to a json file.

jsonFile, _ := json.MarshalIndent(m, "", "\t")
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:

[
	{
		"a1": "aa1",
		"b1": "bb1"
	},
	{
		"a2": "aa2",
		"b2": "bb2"
	},
	{
		"a3": "aa3",
		"b3": "bb3"
	}
]

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

{
	"a1": "aa1",
	"b1": "bb1"
}
{
	"a2": "aa2",
	"b2": "bb2"
}
{
	"a3": "aa3",
	"b3": "bb3"
}

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来逐个编码这些对象。

可以像这样实现:

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "  ")
for _, v := range m {
    if err := enc.Encode(v); err != nil {
        panic(err)
    }
}

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

{
  "a1": "aa1",
  "b1": "bb1"
}
{
  "a2": "aa2",
  "b2": "bb2"
}
{
  "a3": "aa3",
  "b3": "bb3"
}

此示例将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:

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", "  ")
for _, v := range m {
	if err := enc.Encode(v); err != nil {
		panic(err)
	}
}

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

{
	"a1": "aa1",
	"b1": "bb1"
}
{
	"a2": "aa2",
	"b2": "bb2"
}
{
	"a3": "aa3",
	"b3": "bb3"
}

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:

确定