dqwh1203 2014-04-21 10:51
浏览 58
已采纳

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

I want to save some treenode data as json so that I can use it in a web client. The orginal data looks like this:

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

I want to the json file to look like this.

{
  "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"
    }
  ]
}

So far I have this code but I am stumped by the AddtoJson() function.

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, "

")
            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)) 
}

Can anyone help me? Thanks!

  • 写回答

1条回答 默认 最新

  • douge3492 2014-04-21 12:56
    关注

    Something along the lines of 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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据