英文:
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.
{
"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, "\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))
}
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
的结构体,具有Id
、ParentId
、Name
、Leaf
和Children
等字段。Size()
方法用于计算节点及其子节点的数量。Add()
方法用于将节点添加到树中的适当位置。
英文:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论