json - 使用golang将treenode保存为json文件?

标签 json tree go

我想将一些树节点数据保存为 json,以便我可以在 Web 客户端中使用它。原始数据如下所示:

id  parentId    name        leaf
001 000 root            0
002 001 Shooping        0
003 002 Housewares  0
004 003 Kitchen     1
005 003 Officer     1
006 002 Remodeling  0
007 006 Retile kitchen  1
008 006 Paint bedroom   1
009 008 Ceiling         1   
010 006 Other       1
011 001 Misc        1

我希望 json 文件看起来像这样。

{
  "name": "root", 
  "children": [
    {
      "name": "Shopping", 
      "children": [
        {
          "name": "Housewares",
          "children": [
            {
              "name": "Kitchen",
              "leaf": "1"
            },
            {
              "name": "Officer",
              "leaf": "1"
            }
          ]
        },
        {
          "name": "Remodeling",
          "children": [
            {
              "name": "Retile kitchen",
              "leaf": "1"
            },
            {
              "name": "Paint bedroom",
              "children": [
                {
                  "name": "Ceiling",
                  "leaf": "1"
                }
              ]
            },
            {
              "name": "Other",
              "leaf": "1"
            }
          ]
        }
      ]
    },
    {
      "name": "Misc",
      "leaf": "1"
    }
  ]
}

到目前为止,我已经有了这段代码,但我对 AddtoJson() 函数感到困惑。

package main
import (
    "fmt"
 "encoding/json"
)
type Node struct {
    ID string
    Name    string `json:"name"`
    Children []*Node  `json:"children"`
    Leaf  string    `json:"leaf"`
}
var rootNode *Node
func SaveTreetoJson(node []Node, parent string, depth int) {
    for _, r := range node {
        if r.parentID == parent {
            for i := 0; i < depth; i++ {
                AddtoJson(rootNode)//how to deal with the "AddtoJson" function  and the rootNode?
            }
            fmt.Print(r.Name, "\n\n")
            SaveTreetoJson(node, r.ID, depth+1)
        }
    }
}

func main() {
    data := []Node{
{"001","000","root","0"},
{"002","001","Shooping","0"},
{"003","002","Housewares","0"},
{"004","003","Kitchen","1"},
{"005","003","Officer","1"},
{"006","002","Remodeling","0"},
{"007","006","Retile kitchen","1"},
{"008","006","Paint bedroom","1"},
{"009","008","Ceiling","1"},    
{"010","006","Other","1"},
{"011","001","Misc","1"},
    }
    SaveTreetoJson(data, "root", 0)
 bytes, _:= json.Marshal(rootNode)
    fmt.Println(string(bytes)) 
}

谁能帮帮我?谢谢!

最佳答案

类似于 this 的内容:

type Node struct {
    Id       string  `json:"-"`
    ParentId string  `json:"-"`
    Name     string  `json:"name"`
    Leaf     string  `json:"leaf,omitempty"`
    Children []*Node `json:"children,omitempty"`
}

func (this *Node) Size() int {
    var size int = len(this.Children)
    for _, c := range this.Children {
        size += c.Size()
    }
    return size
}

func (this *Node) Add(nodes... *Node) bool {
    var size = this.Size();
    for _, n := range nodes {
        if n.ParentId == this.Id {
            this.Children = append(this.Children, n)
        } else { 
            for _, c := range this.Children {
                if c.Add(n) {
                    break
                }
            }
        }
    }
    return this.Size() == size + len(nodes)
}

关于json - 使用golang将treenode保存为json文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23195899/

相关文章:

java - 如何解决使用 jersey 在 java Rest 服务中生成 xml 的问题?

c++ - 是否有某种算法可以反向过滤N个二叉树节点?

javascript - 我可以将 DOM 视为树形图吗?

go - 使用反射生成Go方法集

mongodb - 如何将 mongodb 投影与 Go 和 mgo 一起使用?

ios - 如何在不冲突的情况下使用开源 iOS 库?

javascript - JSONarray 没有数组名称?

javascript - 如何使用 javascript 中采用 splat 参数的函数解析 json 对象?

algorithm - 将不平衡树转换为生成树

去导入模块 "later"