使用嵌套数组进行JSON编组?

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

Marshaling JSON with nested array?

问题

我想创建以下格式的JSON:

  1. {
  2. "name": "val1",
  3. "version": "val2",
  4. "type": "val3",
  5. "settings": [
  6. "setting1",
  7. "setting2",
  8. "setting3",
  9. [
  10. "option1",
  11. "option2"
  12. ]
  13. ]
  14. }

但我不知道如何在settings内创建嵌套数组:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Config struct {
  7. Name string `json:"name"`
  8. Version string `json:"version"`
  9. Type string `json:"type"`
  10. Settings []string `json:"settings"`
  11. }
  12. func main() {
  13. settings := []string{"setting1", "setting2", "setting3"}
  14. options := []string{"option1", "option2"}
  15. setopts := append(settings, options...)
  16. c := &Config{"val1", "val2", "val3", setopts}
  17. j, err := json.Marshal(c)
  18. if err != nil {
  19. panic(err)
  20. }
  21. fmt.Println(string(j))
  22. }

通过jq输出以提高可读性:

  1. $ ./main | jq
  2. {
  3. "name": "val1",
  4. "version": "val2",
  5. "type": "val3",
  6. "settings": [
  7. "setting1",
  8. "setting2",
  9. "setting3",
  10. "option1",
  11. "option2"
  12. ]
  13. }

结果是option1option2settings数组内的值,但它们应该是settings的嵌套数组内的值。而且,在编组时,选项可能不总是存在,在这些情况下,不应创建嵌套数组。

我该如何实现这个目标?

英文:

I want to create JSON like this:

  1. {
  2. "name": "val1",
  3. "version": "val2",
  4. "type": "val3",
  5. "settings": [
  6. "setting1,
  7. "setting2",
  8. "setting3",
  9. [
  10. "option1",
  11. "option2"
  12. ]
  13. ]
  14. }

But I don't know how to create the nested array inside settings:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Config struct {
  7. Name string `json:"name"`
  8. Version string `json:"version"`
  9. Type string `json:"type"`
  10. Settings []string `json:"settings"`
  11. }
  12. func main() {
  13. settings := []string{"setting1", "setting2", "setting3"}
  14. options := []string{"option1", "option2"}
  15. setopts := append(settings, options...)
  16. c := &Config{"val1", "val2", "val3", setopts}
  17. j, err := json.Marshal(c)
  18. if err != nil {
  19. panic(err)
  20. }
  21. fmt.Println(string(j))
  22. }

Output piped through jq for readability:

  1. $ ./main | jq
  2. {
  3. "name": "val1",
  4. "version": "val2",
  5. "type": "val3",
  6. "settings": [
  7. "setting1",
  8. "setting2",
  9. "setting3",
  10. "option1",
  11. "option2"
  12. ]
  13. }

The result is that option1 and option2 is values inside settings array, but they should be inside a nested array of settings instead. Also, the options may not always be present when marshaling and in those cases the nested array should not be created.

How can I accomplish this?

答案1

得分: 4

你在这里展示的输出可以被描述为 JSON 字符串或字符串数组。

你可以使用 []interface{} 来建模它,并向其附加字符串或切片:

  1. type Config struct {
  2. Name string `json:"name"`
  3. Version string `json:"version"`
  4. Type string `json:"type"`
  5. Settings []interface{} `json:"settings"`
  6. }
  7. func main() {
  8. settings := []interface{}{
  9. "setting1",
  10. "setting2",
  11. "setting3",
  12. []string{"option1", "option2"},
  13. }
  14. c := &Config{"val1", "val2", "val3", settings}
  15. j, err := json.Marshal(c)
  16. if err != nil {
  17. panic(err)
  18. }
  19. fmt.Println(string(j))
  20. }

Playground: https://go.dev/play/p/8LPAVBPmd8w

英文:

The output that you show here:

  1. "setting1",
  2. "setting2",
  3. "setting3",
  4. [
  5. "option1",
  6. "option2"
  7. ]

can be described as JSON string or array-of-strings.

You may model this with []interface{}, and append to it either strings or slices:

  1. type Config struct {
  2. Name string `json:"name"`
  3. Version string `json:"version"`
  4. Type string `json:"type"`
  5. Settings []interface{} `json:"settings"`
  6. }
  7. func main() {
  8. settings := []interface{}{
  9. "setting1",
  10. "setting2",
  11. "setting3",
  12. []string{"option1", "option2"},
  13. }
  14. c := &Config{"val1", "val2", "val3", settings}
  15. j, err := json.Marshal(c)
  16. if err != nil {
  17. panic(err)
  18. }
  19. fmt.Println(string(j))
  20. }

Playground: https://go.dev/play/p/8LPAVBPmd8w

huangapple
  • 本文由 发表于 2021年12月13日 04:35:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/70327490.html
匿名

发表评论

匿名网友

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

确定