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

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

Using golang to save treenode as a json file?

问题

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

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. type Node struct {
  7. ID string `json:"id"`
  8. ParentID string `json:"parentId"`
  9. Name string `json:"name"`
  10. Leaf int `json:"leaf"`
  11. Children []*Node `json:"children,omitempty"`
  12. }
  13. var rootNode *Node
  14. func AddToJson(node *Node, parentID string) {
  15. if rootNode == nil {
  16. rootNode = node
  17. return
  18. }
  19. if node.ParentID == parentID {
  20. for _, child := range rootNode.Children {
  21. if child.ID == node.ID {
  22. return
  23. }
  24. }
  25. rootNode.Children = append(rootNode.Children, node)
  26. return
  27. }
  28. for _, child := range rootNode.Children {
  29. AddToJson(node, child.ID)
  30. }
  31. }
  32. func SaveTreeToJson(nodes []*Node, parentID string, depth int) {
  33. for _, node := range nodes {
  34. if node.ParentID == parentID {
  35. for i := 0; i < depth; i++ {
  36. AddToJson(node, parentID)
  37. }
  38. fmt.Println(node.Name)
  39. SaveTreeToJson(nodes, node.ID, depth+1)
  40. }
  41. }
  42. }
  43. func main() {
  44. data := []*Node{
  45. {"001", "000", "root", 0, nil},
  46. {"002", "001", "Shopping", 0, nil},
  47. {"003", "002", "Housewares", 0, nil},
  48. {"004", "003", "Kitchen", 1, nil},
  49. {"005", "003", "Office", 1, nil},
  50. {"006", "002", "Remodeling", 0, nil},
  51. {"007", "006", "Retile kitchen", 1, nil},
  52. {"008", "006", "Paint bedroom", 1, nil},
  53. {"009", "008", "Ceiling", 1, nil},
  54. {"010", "006", "Other", 1, nil},
  55. {"011", "001", "Misc", 1, nil},
  56. }
  57. SaveTreeToJson(data, "000", 0)
  58. bytes, _ := json.Marshal(rootNode)
  59. fmt.Println(string(bytes))
  60. }

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

英文:

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:

  1. id parentId name leaf
  2. 001 000 root 0
  3. 002 001 Shooping 0
  4. 003 002 Housewares 0
  5. 004 003 Kitchen 1
  6. 005 003 Officer 1
  7. 006 002 Remodeling 0
  8. 007 006 Retile kitchen 1
  9. 008 006 Paint bedroom 1
  10. 009 008 Ceiling 1
  11. 010 006 Other 1
  12. 011 001 Misc 1

I want to the json file to look like this.

  1. {
  2. &quot;name&quot;: &quot;root&quot;,
  3. &quot;children&quot;: [
  4. {
  5. &quot;name&quot;: &quot;Shopping&quot;,
  6. &quot;children&quot;: [
  7. {
  8. &quot;name&quot;: &quot;Housewares&quot;,
  9. &quot;children&quot;: [
  10. {
  11. &quot;name&quot;: &quot;Kitchen&quot;,
  12. &quot;leaf&quot;: &quot;1&quot;
  13. },
  14. {
  15. &quot;name&quot;: &quot;Officer&quot;,
  16. &quot;leaf&quot;: &quot;1&quot;
  17. }
  18. ]
  19. },
  20. {
  21. &quot;name&quot;: &quot;Remodeling&quot;,
  22. &quot;children&quot;: [
  23. {
  24. &quot;name&quot;: &quot;Retile kitchen&quot;,
  25. &quot;leaf&quot;: &quot;1&quot;
  26. },
  27. {
  28. &quot;name&quot;: &quot;Paint bedroom&quot;,
  29. &quot;children&quot;: [
  30. {
  31. &quot;name&quot;: &quot;Ceiling&quot;,
  32. &quot;leaf&quot;: &quot;1&quot;
  33. }
  34. ]
  35. },
  36. {
  37. &quot;name&quot;: &quot;Other&quot;,
  38. &quot;leaf&quot;: &quot;1&quot;
  39. }
  40. ]
  41. }
  42. ]
  43. },
  44. {
  45. &quot;name&quot;: &quot;Misc&quot;,
  46. &quot;leaf&quot;: &quot;1&quot;
  47. }
  48. ]
  49. }

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

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;encoding/json&quot;
  5. )
  6. type Node struct {
  7. ID string
  8. Name string `json:&quot;name&quot;`
  9. Children []*Node `json:&quot;children&quot;`
  10. Leaf string `json:&quot;leaf&quot;`
  11. }
  12. var rootNode *Node
  13. func SaveTreetoJson(node []Node, parent string, depth int) {
  14. for _, r := range node {
  15. if r.parentID == parent {
  16. for i := 0; i &lt; depth; i++ {
  17. AddtoJson(rootNode)//how to deal with the &quot;AddtoJson&quot; function and the rootNode?
  18. }
  19. fmt.Print(r.Name, &quot;\n\n&quot;)
  20. SaveTreetoJson(node, r.ID, depth+1)
  21. }
  22. }
  23. }
  24. func main() {
  25. data := []Node{
  26. {&quot;001&quot;,&quot;000&quot;,&quot;root&quot;,&quot;0&quot;},
  27. {&quot;002&quot;,&quot;001&quot;,&quot;Shooping&quot;,&quot;0&quot;},
  28. {&quot;003&quot;,&quot;002&quot;,&quot;Housewares&quot;,&quot;0&quot;},
  29. {&quot;004&quot;,&quot;003&quot;,&quot;Kitchen&quot;,&quot;1&quot;},
  30. {&quot;005&quot;,&quot;003&quot;,&quot;Officer&quot;,&quot;1&quot;},
  31. {&quot;006&quot;,&quot;002&quot;,&quot;Remodeling&quot;,&quot;0&quot;},
  32. {&quot;007&quot;,&quot;006&quot;,&quot;Retile kitchen&quot;,&quot;1&quot;},
  33. {&quot;008&quot;,&quot;006&quot;,&quot;Paint bedroom&quot;,&quot;1&quot;},
  34. {&quot;009&quot;,&quot;008&quot;,&quot;Ceiling&quot;,&quot;1&quot;},
  35. {&quot;010&quot;,&quot;006&quot;,&quot;Other&quot;,&quot;1&quot;},
  36. {&quot;011&quot;,&quot;001&quot;,&quot;Misc&quot;,&quot;1&quot;},
  37. }
  38. SaveTreetoJson(data, &quot;root&quot;, 0)
  39. bytes, _:= json.Marshal(rootNode)
  40. fmt.Println(string(bytes))
  41. }

Can anyone help me? Thanks!

答案1

得分: 5

以下是this的类似代码:

  1. type Node struct {
  2. Id string `json:"-"`
  3. ParentId string `json:"-"`
  4. Name string `json:"name"`
  5. Leaf string `json:"leaf,omitempty"`
  6. Children []*Node `json:"children,omitempty"`
  7. }
  8. func (this *Node) Size() int {
  9. var size int = len(this.Children)
  10. for _, c := range this.Children {
  11. size += c.Size()
  12. }
  13. return size
  14. }
  15. func (this *Node) Add(nodes... *Node) bool {
  16. var size = this.Size();
  17. for _, n := range nodes {
  18. if n.ParentId == this.Id {
  19. this.Children = append(this.Children, n)
  20. } else {
  21. for _, c := range this.Children {
  22. if c.Add(n) {
  23. break
  24. }
  25. }
  26. }
  27. }
  28. return this.Size() == size + len(nodes)
  29. }

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

英文:

Something along the lines of this :

  1. type Node struct {
  2. Id string `json:&quot;-&quot;`
  3. ParentId string `json:&quot;-&quot;`
  4. Name string `json:&quot;name&quot;`
  5. Leaf string `json:&quot;leaf,omitempty&quot;`
  6. Children []*Node `json:&quot;children,omitempty&quot;`
  7. }
  8. func (this *Node) Size() int {
  9. var size int = len(this.Children)
  10. for _, c := range this.Children {
  11. size += c.Size()
  12. }
  13. return size
  14. }
  15. func (this *Node) Add(nodes... *Node) bool {
  16. var size = this.Size();
  17. for _, n := range nodes {
  18. if n.ParentId == this.Id {
  19. this.Children = append(this.Children, n)
  20. } else {
  21. for _, c := range this.Children {
  22. if c.Add(n) {
  23. break
  24. }
  25. }
  26. }
  27. }
  28. return this.Size() == size + len(nodes)
  29. }

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:

确定