dongwen2162 2017-11-01 15:40
浏览 87
已采纳

将结构转换为json数组而不是json对象

I apologise in advance if this question is considered too easy or whatever; this is the first time I'm writing anything in go. I have two structs (simplified for this question)

type A struct {
    Content string
}

type B struct {
    Element A `json:"0"`
    Children []B `json:"1"`
}

I want to encode a value of type B into JSON, but instead of returning an object it should return a json array

For example:

What I get:

[
    {
        "0":{
            "Content":"AAA"
        },
        "1":[
            {
                "0":{
                    "Content":"BBB"
                },
                "1":[
                    {
                        "0":{
                            "Content":"CCC"
                        },
                        "1":[]
                    },
                    {
                        "0":{
                            "Content":"DDD"
                        },
                        "1":[]
                    }
                }
            ]
        ]
    }
]

What I need:

[
    {"Content": "AAA"}, 
    [
        [
            {"Content": "BBB"},
            [
                {"Content": "CCC"}, 
                []
            ]
        ], 
        [
            {"Content": "DDD"}, 
            []
        ]
    ]
]

I could do this by manually iterating through it, but I would hope that there's an integrated way to do it

  • 写回答

2条回答 默认 最新

  • douchensou6495 2017-11-02 05:50
    关注

    You may do so by implementing json.Marshaler interface in B.

    For example: https://play.golang.org/p/fT1WlQ5Raz

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    type A struct {
        Content string
    }
    
    type B struct {
        Element  A
        Children []B
    }
    
    // MarshalJSON implements json.Marshaler
    func (b B) MarshalJSON() ([]byte, error) {
        return json.Marshal([]interface{}{
            b.Element,
            b.Children,
        })
    }
    
    func main() {
    
        root := B{
            Element: A{Content: "AAA"},
            Children: []B{
                B{
                    Element: A{Content: "BBB"},
                    Children: []B{
                        B{Element: A{Content: "CCC"}, Children: []B{}},
                        B{Element: A{Content: "DDD"}, Children: []B{}},
                    },
                },
            },
        }
    
        content, _ := json.MarshalIndent(root, "", "  ")
        fmt.Printf("%s
    ", content)
    }
    

    Results:

    [
      {
        "Content": "AAA"
      },
      [
        [
          {
            "Content": "BBB"
          },
          [
            [
              {
                "Content": "CCC"
              },
              []
            ],
            [
              {
                "Content": "DDD"
              },
              []
            ]
          ]
        ]
      ]
    ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS设计小型化180度耦合器
  • ¥15 使用CInternetSession,CHttpFile读取网页文件时有些电脑上会卡住怎么办?
  • ¥15 水下机器人的半物理仿真研究
  • ¥15 微服务假死,一段时间后自动恢复,如何排查处理
  • ¥50 webrtc-streamer TCP rtsp
  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计