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 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛