加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 09:23:43 所属栏目:大数据 来源:网络整理
导读:我想将一些treenode数据保存为json,以便我可以在Web客户端中使用它.原始数据如下所示: id parentId name leaf001 000 root 0002 001 Shooping 0003 002 Housewares 0004 003 Kitchen 1005 003 Officer 1006 002 Remodeling 0007 006 Retile kitchen 1008 00
我想将一些treenode数据保存为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",{
              "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,"nn")
            SaveTreetoJson(node,r.ID,depth+1)
        }
    }
}

func main() {
    data := []Node{
{"001","000","root","0"},{"002","001","Shooping",{"003","002","Housewares",{"004","003","Kitchen","1"},{"005","Officer",{"006","Remodeling",{"007","006","Retile kitchen",{"008","Paint bedroom",{"009","008","Ceiling",{"010","Other",{"011","Misc",}
    SaveTreetoJson(data,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)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读