很少說了直接上代碼json
要引用 using LitJson;微信
// 根據Menu列表生成符合微信規範的建立菜單JSon //一級菜單不超過3個,二級菜單不超過5個 //微信惟一標示 //菜單列表,type爲click時url留空,view時key留空 public string CreateMenuJson() { DataTable table = new wxMenuModels().getTable(); DataRow[] dt = table.Select("wx_parent=0"); JsonData jd = new JsonData(); jd["button"] = new JsonData(); foreach(DataRow h in dt) { if(Convert.ToInt32(h["IsSub"])==1)//有子菜單 { JsonData json = new JsonData(); json["name"] = h["wx_name"].ToString(); json["sub_button"] = new JsonData(); DataRow[] dtt = table.Select("wx_parent="+h["wx_ID"].ToString()); foreach(DataRow hh in dtt) { JsonData tempJson = new JsonData(); tempJson["type"] = hh["wx_type"].ToString(); tempJson["name"] = hh["wx_name"].ToString(); if(!string.IsNullOrEmpty(hh["wx_key"].ToString())) { tempJson["key"] = hh["wx_key"].ToString(); } else { tempJson["url"] = hh["wx_url"].ToString(); } json["sub_button"].Add(tempJson); } jd["button"].Add(json); } else //無子菜單 { JsonData tempJson = new JsonData(); tempJson["type"] = h["wx_type"].ToString(); tempJson["name"] = h["wx_name"].ToString(); if (!string.IsNullOrEmpty(h["wx_key"].ToString())) { tempJson["key"] = h["wx_key"].ToString(); } else { tempJson["url"] = h["wx_url"].ToString(); } jd["button"].Add(tempJson); } } return jd.ToJson(); }