ds122455 2016-01-17 17:29
浏览 66
已采纳

解析嵌套列表不会降级到3级

I have a JSON representing menu items.

A menu item can have a sub menu item, which in turn can have another sub menu item and so son.

The input JSON relates the menu items through a parent id. I'm trying to convert this to a model where each menu item has a slice of its sub menu items.

The sub menus go three levels deep. I've managed to parse upto two levels but I have no idea why the third level isn't being parsed. I've been debugging this problem for hours. I would appreciate some help.

menu2.sjon

[
  {
    "category_id": 4,
    "category_id_400": "'SCHOO",
    "name": "School Supplies",
    "parent_id": 2,
    "position": 2,
    "level": 2,
    "status": 1,
    "url": "http://www.booksrus.kw/sa-en/school-supplies.html"
  },
  {
    "category_id": 141,
    "category_id_400": "'SCHBA",
    "name": "School Bags",
    "parent_id": 4,
    "position": 12,
    "level": 3,
    "status": 1,
    "url": "http://www.booksrus.kw/sa-en/school-supplies/school-bags.html"
  },
  {
    "category_id": 269,
    "category_id_400": "'AEP",
    "name": "Bags Knapsack with Trolley",
    "parent_id": 141,
    "position": 1,
    "level": 4,
    "status": 1,
    "url": "http://www.booksrus.kw/sa-en/school-supplies/school-bags/bags-knapsack-with-trolley.html"
  }
]

menu.go

package main

import(
    "fmt"
    "encoding/json"
    "io/ioutil"
    "sort"
    "bytes"
)

type MenuItems []MenuItem

func (a MenuItems) Len() int           { return len(a) }
func (a MenuItems) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a MenuItems) Less(i, j int) bool { return a[i].Category_id < a[j].Category_id }

type MenuItem struct{
    Category_id int `json:"category_id"`
    Category_id_400 string `json:"category_id_400"`
    Name string `json:"name"`
    Parent_id int `json:"parent_id"`
    Position int `json:"position"`
    Level int `json:"level"`
    Status int `json:"status"`
    Url string  `json:"url"`
    Subs []MenuItem `json:"subs"`
}

func (m MenuItem) String() string{

     var buffer bytes.Buffer
     buffer.WriteString(fmt.Sprintf("%d %s
",m.Category_id,m.Name))
    for _,s := range m.Subs{
        buffer.WriteString(fmt.Sprintf(">   %s
",s.String()));
    }

    return buffer.String()
    //return fmt.Sprintf("CategoryId: %d, ParentId: %d,Name: %s, Sub: %v
",m.Category_id,m.Parent_id,m.Name,m.Subs);
}

func (m *MenuItem) TryAdd(other MenuItem) bool{

    if other.Parent_id == m.Category_id {

        m.Subs = append(m.Subs,other);
        return true
    }else{
        for _,sub := range m.Subs{
            if found := sub.TryAdd(other);found{
                return true
            }
        }
    }

    return false
}

func main() {
    rootItems := make([]MenuItem,0)
    bytes, err := ioutil.ReadFile("menu2.json")

    if err != nil{
        fmt.Printf("Reading: %s
",err.Error())
        return;
    }

    var menuItems []MenuItem
    err = json.Unmarshal(bytes,&menuItems)

    if err != nil{
        fmt.Println(err.Error())
        return
    }

    sort.Sort(MenuItems(menuItems))

    for _,item := range menuItems{
        if item.Parent_id == 2{
            rootItems = append(rootItems,item)
        }else{
            for i:=0;i<len(rootItems);i++{
                if found := rootItems[i].TryAdd(item); found{
                    break;
                }else{
                    fmt.Printf("No Action: Id: %d, Name: %s, Parent: %d.
",item.Category_id,item.Name,item.Parent_id)
                }
            }
        }
    }

    fmt.Printf("
Rootitems:
%s
",rootItems)
}

Output

Rootitems:
[4           School Supplies
>   141           School Bags
//Third level should appear here
]
  • 写回答

1条回答 默认 最新

  • douwo5710 2016-01-17 17:59
    关注

    This loop in the TryAdd function is most probably your issue:

    for _, sub := range m.Subs {
        if found := sub.TryAdd(other); found {
            return true
        }
    }
    

    The sub variable in this loop is actually a copy of the slice element. Any changes you make there will not persist back to the the element that is stored in the slice.

    You should be able to solve this issue by not working with the element's copy, but referencing it by its index instead:

    for i := range m.Subs {
        if found := m.Subs[i].TryAdd(other); found {
            return true
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看