此例用 微信的 设置菜单为例 底部三个菜单, 每个菜单里三个子菜单
定义结构体
type Btn struct{
Name string `json:"name"`
Type string `json:"type"`
Url string `json:"url"`
Sub_button []Btn `json:"sub_button,omitempty"` //值为空时 直接忽略
UnShow string `json"-"` //忽略字段
}
type menu struct{
Button []Btn `json:"button"`
}
结构体赋值
jsonData := Menu{
Button:[]Btn{
{Name:"home",Type:"view",Url:"https://www.qq.com/auth"},
{Name:"tool",Sub_button:[]Btn{
{Name:"a1",Type:"view",Url:"https://www.qq.com/a1"},
{Name:"a2",Type:"view",Url:"https://www.qq.com/a2"},
{Name:"a3",Type:"view",Url:"https://www.qq.com/a3"},
}},
{Name:"other",Sub_button:[]Btn{
{Name:"a1",Type:"view",Url:"https://www.qq.com/a1"},
{Name:"a2",Type:"view",Url:"https://www.qq.com/a2"},
{Name:"a3",Type:"view",Url:"https://www.qq.com/a3"},
}},
},
}
结构体转json
str,err := json.Marshal(jsonData)
if err != nil{
panic(err)
}
fmt.println(string(str))
map[string]interface{}
param := map[string]interface{}{
"button":[]Btn{
{Name:"home",Type:"view",Url:"https://www.qq.com/auth"},
{Name:"tool",Sub_button:[]Btn{
{Name:"a1",Type:"view",Url:"https://www.qq.com/1"},
{Name:"a1",Type:"view",Url:"https://www.qq.com/1"},
{Name:"a1",Type:"view",Url:"https://www.qq.com/1"},
}},
{Name:"other",Sub_button:[]Btn{
{Name:"a2",Type:"view",Url:"https://www.qq.com/a2"},
{Name:"a2",Type:"view",Url:"https://www.qq.com/a2"},
{Name:"a2",Type:"view",Url:"https://www.qq.com/a2"},
}},
},
}