json.Marshal和json.MarshalIndent在Go语言中有什么区别?

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

What is the difference between json.Marshal and json.MarshalIndent using Go?

问题

我想要以JSON格式输出CF命令的结果,但我不确定应该使用json.Marshal还是json.MarshalIndent

我需要的输出格式如下:

  1. {
  2. "key1": "value1",
  3. ....
  4. "keyn": "valuen"
  5. }

这是一个旧的示例,但它不是我想要的输出格式:

  1. cmd.ui.Say(terminal.EntityNameColor(T("User-Provided:")))
  2. for _, key := range keys {
  3. // cmd.ui.Say("%s: %v", key, envVars[key])
  4. 这里需要一个新的示例,使用json.MarshalIndent
  5. }

我从未使用过Go语言,所以我真的不知道应该使用哪个函数以及如何使用。

英文:

I would like to get an output of a CF command in JSON format but I am not sure what to use either json.Marshal or json.MarshalIndent.

The output I need is like this:

  1. {
  2. "key1": "value1",
  3. ....
  4. "keyn": "valuen",
  5. }

This is the old example but it is not the desired output:

  1. cmd.ui.Say(terminal.EntityNameColor(T("User-Provided:")))
  2. for _, key := range keys {
  3. // cmd.ui.Say("%s: %v", key, envVars[key])
  4. here needed a new one with json.marshalIdent
  5. }

I never used go so I really do not know which one to use and how.

答案1

得分: 16

我认为文档对此非常清楚。json.Marshal()json.MarshalIndent()都会生成一个 JSON 文本结果(以[]byte形式),但前者会以紧凑的方式输出,没有缩进,而后者则应用了(可以自定义的)缩进。引用自json.MarshalIndent()的文档:

> MarshalIndent 类似于 Marshal,但会应用 Indent 来格式化输出。

看下面这个简单的例子:

  1. type Entry struct {
  2. Key string `json:"key"`
  3. }
  4. e := Entry{Key: "value"}
  5. res, err := json.Marshal(e)
  6. fmt.Println(string(res), err)
  7. res, err = json.MarshalIndent(e, "", " ")
  8. fmt.Println(string(res), err)

输出结果是(在Go Playground上试一试):

  1. {"key":"value"} <nil>
  2. {
  3. "key": "value"
  4. } <nil>

还有json.Encoder

  1. type Entry struct {
  2. Key string `json:"key"`
  3. }
  4. e := Entry{Key: "value"}
  5. enc := json.NewEncoder(os.Stdout)
  6. if err := enc.Encode(e); err != nil {
  7. panic(err)
  8. }
  9. enc.SetIndent("", " ")
  10. if err := enc.Encode(e); err != nil {
  11. panic(err)
  12. }

输出结果是(在Go Playground上试一试):

  1. {"key":"value"}
  2. {
  3. "key": "value"
  4. }
英文:

I think the doc is pretty clear on this. Both json.Marshal()
and json.MarshalIndent() produces a JSON text result (in the form of a []byte), but while the former does a compact output without indentation, the latter applies (somewhat customizable) indent. Quoting from doc of json.MarshalIndent():

> MarshalIndent is like Marshal but applies Indent to format the output.

See this simple example:

  1. type Entry struct {
  2. Key string `json:&quot;key&quot;`
  3. }
  4. e := Entry{Key: &quot;value&quot;}
  5. res, err := json.Marshal(e)
  6. fmt.Println(string(res), err)
  7. res, err = json.MarshalIndent(e, &quot;&quot;, &quot; &quot;)
  8. fmt.Println(string(res), err)

The output is (try it on the Go Playground):

  1. {&quot;key&quot;:&quot;value&quot;} &lt;nil&gt;
  2. {
  3. &quot;key&quot;: &quot;value&quot;
  4. } &lt;nil&gt;

There is also json.Encoder:

  1. type Entry struct {
  2. Key string `json:&quot;key&quot;`
  3. }
  4. e := Entry{Key: &quot;value&quot;}
  5. enc := json.NewEncoder(os.Stdout)
  6. if err := enc.Encode(e); err != nil {
  7. panic(err)
  8. }
  9. enc.SetIndent(&quot;&quot;, &quot; &quot;)
  10. if err := enc.Encode(e); err != nil {
  11. panic(err)
  12. }

Output (try this one on the Go Playground):

  1. {&quot;key&quot;:&quot;value&quot;}
  2. {
  3. &quot;key&quot;: &quot;value&quot;
  4. }

huangapple
  • 本文由 发表于 2017年6月12日 17:07:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/44495489.html
匿名

发表评论

匿名网友

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

确定