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

huangapple go评论79阅读模式
英文:

Using golang to save treenode as a json file?

问题

你好!以下是你提供的代码的翻译版本:

package main

import (
	"encoding/json"
	"fmt"
)

type Node struct {
	ID       string  `json:"id"`
	ParentID string  `json:"parentId"`
	Name     string  `json:"name"`
	Leaf     int     `json:"leaf"`
	Children []*Node `json:"children,omitempty"`
}

var rootNode *Node

func AddToJson(node *Node, parentID string) {
	if rootNode == nil {
		rootNode = node
		return
	}
	if node.ParentID == parentID {
		for _, child := range rootNode.Children {
			if child.ID == node.ID {
				return
			}
		}
		rootNode.Children = append(rootNode.Children, node)
		return
	}
	for _, child := range rootNode.Children {
		AddToJson(node, child.ID)
	}
}

func SaveTreeToJson(nodes []*Node, parentID string, depth int) {
	for _, node := range nodes {
		if node.ParentID == parentID {
			for i := 0; i < depth; i++ {
				AddToJson(node, parentID)
			}
			fmt.Println(node.Name)
			SaveTreeToJson(nodes, node.ID, depth+1)
		}
	}
}

func main() {
	data := []*Node{
		{"001", "000", "root", 0, nil},
		{"002", "001", "Shopping", 0, nil},
		{"003", "002", "Housewares", 0, nil},
		{"004", "003", "Kitchen", 1, nil},
		{"005", "003", "Office", 1, nil},
		{"006", "002", "Remodeling", 0, nil},
		{"007", "006", "Retile kitchen", 1, nil},
		{"008", "006", "Paint bedroom", 1, nil},
		{"009", "008", "Ceiling", 1, nil},
		{"010", "006", "Other", 1, nil},
		{"011", "001", "Misc", 1, nil},
	}

	SaveTreeToJson(data, "000", 0)

	bytes, _ := json.Marshal(rootNode)
	fmt.Println(string(bytes))
}

希望对你有所帮助!如果你有任何其他问题,请随时问我。

英文:

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.

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

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

package main
import (
&quot;fmt&quot;
&quot;encoding/json&quot;
)
type Node struct {
ID string
Name    string `json:&quot;name&quot;`
Children []*Node  `json:&quot;children&quot;`
Leaf  string 	`json:&quot;leaf&quot;`
}
var rootNode *Node
func SaveTreetoJson(node []Node, parent string, depth int) {
for _, r := range node {
if r.parentID == parent {
for i := 0; i &lt; depth; i++ {
AddtoJson(rootNode)//how to deal with the &quot;AddtoJson&quot; function  and the rootNode?
}
fmt.Print(r.Name, &quot;\n\n&quot;)
SaveTreetoJson(node, r.ID, depth+1)
}
}
}
func main() {
data := []Node{
{&quot;001&quot;,&quot;000&quot;,&quot;root&quot;,&quot;0&quot;},
{&quot;002&quot;,&quot;001&quot;,&quot;Shooping&quot;,&quot;0&quot;},
{&quot;003&quot;,&quot;002&quot;,&quot;Housewares&quot;,&quot;0&quot;},
{&quot;004&quot;,&quot;003&quot;,&quot;Kitchen&quot;,&quot;1&quot;},
{&quot;005&quot;,&quot;003&quot;,&quot;Officer&quot;,&quot;1&quot;},
{&quot;006&quot;,&quot;002&quot;,&quot;Remodeling&quot;,&quot;0&quot;},
{&quot;007&quot;,&quot;006&quot;,&quot;Retile kitchen&quot;,&quot;1&quot;},
{&quot;008&quot;,&quot;006&quot;,&quot;Paint bedroom&quot;,&quot;1&quot;},
{&quot;009&quot;,&quot;008&quot;,&quot;Ceiling&quot;,&quot;1&quot;},	
{&quot;010&quot;,&quot;006&quot;,&quot;Other&quot;,&quot;1&quot;},
{&quot;011&quot;,&quot;001&quot;,&quot;Misc&quot;,&quot;1&quot;},
}
SaveTreetoJson(data, &quot;root&quot;, 0)
bytes, _:= json.Marshal(rootNode)
fmt.Println(string(bytes)) 
}

Can anyone help me? Thanks!

答案1

得分: 5

以下是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)
}

这段代码定义了一个名为Node的结构体,具有IdParentIdNameLeafChildren等字段。Size()方法用于计算节点及其子节点的数量。Add()方法用于将节点添加到树中的适当位置。

英文:

Something along the lines of this :

type Node struct {
Id       string  `json:&quot;-&quot;`
ParentId string  `json:&quot;-&quot;`
Name     string  `json:&quot;name&quot;`
Leaf     string  `json:&quot;leaf,omitempty&quot;`
Children []*Node `json:&quot;children,omitempty&quot;`
}
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)
}

huangapple
  • 本文由 发表于 2014年4月21日 18:51:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/23195899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定