/// <summary> /// 根据微信返回的Json数据得到可用的GetMenuResult结果 /// </summary> /// <param name="resultFull"></param> /// <returns></returns> public static GetMenuResult GetMenuFromJsonResult(GetMenuResultFull resultFull) { GetMenuResult result = null; try { //重新整理按钮信息 ButtonGroup bg = new ButtonGroup(); foreach (var rootButton in resultFull.menu.button) { if (rootButton.name == null) { continue;//没有设置一级菜单 } var availableSubButton = rootButton.sub_button.Count(z => !string.IsNullOrEmpty(z.name));//可用二级菜单按钮数量 if (availableSubButton == 0) { //底部单击按钮 if (rootButton.type == null || (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(rootButton.key))) { throw new WXMenuException("单击按钮的key不能为空!"); } if (rootButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)) { //点击 bg.button.Add(new SingleClickButton() { name = rootButton.name, key = rootButton.key, type = rootButton.type }); } else { //URL bg.button.Add(new SingleViewButton() { name = rootButton.name, url = rootButton.url, type = rootButton.type }); } } else if (availableSubButton < 2) { throw new WXMenuException("子菜单至少需要填写2个!"); } else { //底部二级菜单 var subButton = new SubButton(rootButton.name); bg.button.Add(subButton); foreach (var subSubButton in rootButton.sub_button) { if (subSubButton.name == null) { continue; //没有设置菜单 } if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(subSubButton.key)) { throw new WXMenuException("单击按钮的key不能为空!"); } if (subSubButton.type.Equals("CLICK", StringComparison.OrdinalIgnoreCase)) { //点击 subButton.sub_button.Add(new SingleClickButton() { name = subSubButton.name, key = subSubButton.key, type = subSubButton.type }); } else { //URL subButton.sub_button.Add(new SingleViewButton() { name = subSubButton.name, url = subSubButton.url, type = subSubButton.type }); } } } } if (bg.button.Count < 2) { throw new WXMenuException("一级菜单按钮至少为2个!"); } result = new GetMenuResult() { menu = bg }; } catch (Exception ex) { throw new WXMenuException(ex.Message, ex); } return result; }
public static GetMenuResult GetMenuFromJson(string jsonString) { var finalResult = new GetMenuResult(); try { //@"{""menu"":{""button"":[{""type"":""click"",""name"":""单击测试"",""key"":""OneClick"",""sub_button"":[]},{""name"":""二级菜单"",""sub_button"":[{""type"":""click"",""name"":""返回文本"",""key"":""SubClickRoot_Text"",""sub_button"":[]},{""type"":""click"",""name"":""返回图文"",""key"":""SubClickRoot_News"",""sub_button"":[]},{""type"":""click"",""name"":""返回音乐"",""key"":""SubClickRoot_Music"",""sub_button"":[]}]}]}}" object jsonResult = null; JavaScriptSerializer js = new JavaScriptSerializer(); jsonResult = js.Deserialize<object>(jsonString); var fullResult = jsonResult as Dictionary<string, object>; if (fullResult != null && fullResult.ContainsKey("menu")) { //得到菜单 var menu = fullResult["menu"]; var buttons = (menu as Dictionary<string, object>)["button"] as object[]; foreach (var rootButton in buttons) { var fullButton = rootButton as Dictionary<string, object>; if (fullButton.ContainsKey("key") && !string.IsNullOrEmpty(fullButton["key"] as string)) { //按钮,无下级菜单 finalResult.menu.button.Add(GetSingleButtonFromJsonObject(fullButton)); } else { //二级菜单 var subButton = new SubButton(fullButton["name"] as string); finalResult.menu.button.Add(subButton); foreach (var sb in fullButton["sub_button"] as object[]) { subButton.sub_button.Add(GetSingleButtonFromJsonObject(sb as Dictionary<string, object>)); } } } } else if (fullResult != null && fullResult.ContainsKey("errmsg")) { //菜单不存在 throw new ErrorJsonResultException(fullResult["errmsg"] as string, null, null); } } catch (ErrorJsonResultException ex) { finalResult = null; //如果没有惨淡会返回错误代码:46003:menu no exist } catch (Exception ex) { //其他异常 finalResult = null; } return finalResult; }