dongwen2162 2017-11-01 07: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-01 21: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部