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 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: