我有一個 url 列表,需要將其更改為 JSON 樹結(jié)構(gòu)。到目前為止,我已經(jīng)嘗試了幾種不同的方法,但都失敗了。目前我正在嘗試將此算法轉(zhuǎn)換為 Golang,但它沒有做任何事情,我被困在他們的邏輯中,我到 golang 的轉(zhuǎn)換是有缺陷的,我被困住了。樹.gopackage treeimport (? ? "fmt")type TreeNode struct {? ? Path? ? ?string? ? ?`json:"path"`? ? Children []TreeNode `json:"children"`}urls := []string{"fenceprosc.com/hello-world/#respond","fenceprosc.com/199-2/12615753_1208442715850011_4650003168465028857_o/#respond","fenceprosc.com/199-2/28339_127294957298131_7361568_n/#main","fenceprosc.com/199-2/10917425_1025362007491417_2370903639534256456_n/#main","fenceprosc.com/199-2/vinyl-gate-2/#main","fenceprosc.com/199-2/vinyl-gate1/#main",}func GetTheParentNodeChildArray(path string, treeNodes *[]TreeNode) []TreeNode {? ? for _, treeNode := range *treeNodes {? ? ? ? if len(treeNode.Children) > 0 {? ? ? ? ? ? var possibleParent = false? ? ? ? ? ? for _, node := range treeNode.Children {? ? ? ? ? ? ? ? // What do I do here???? ? ? ? ? ? ? ? fmt.Println("node::", node)? ? ? ? ? ? }? ? ? ? ? ? if possibleParent {? ? ? ? ? ? ? ? return GetTheParentNodeChildArray(path, &treeNode.Children)? ? ? ? ? ? }? ? ? ? }? ? }? ? return *treeNodes}// Add an item node in the tree, at the right positionfunc addToTree(node TreeNode, treeNodes *[]TreeNode) {? ? parentNodeArr := GetTheParentNodeChildArray(node.Path, treeNodes)? ? fmt.Println("parentNodeArr", parentNodeArr)? ? if parentNodeArr == nil {? ? ? ? parentNodeArr = *treeNodes? ? }? ? parentNodeArr = append(parentNodeArr, TreeNode{? ? ? ? Path: node.Path,? ? })}//Create the item tree starting from urlsfunc createTree(nodes []TreeNode) []TreeNode {? ? var tree []TreeNode? ? for _, node := range nodes {? ? ? ? addToTree(node, &tree)? ? }? ? return tree}我也對任何其他解決方案完全開放,我對這個算法沒有任何執(zhí)著。只是很難弄清楚如何將它們構(gòu)建成一棵樹。
Golang 將 URL 片段轉(zhuǎn)換為 JSON 站點樹
寶慕林4294392
2023-08-14 17:26:13