ds122455
ds122455
2016-01-17 17:29

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

  • json
  • recursion

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条回答

为你推荐