表的结构,如以下:

SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[T_4s_Menu](
[F_MenuID] [int] IDENTITY(1,1) NOT NULL,
[F_MenuName] [nvarchar](50) NULL,
[F_MenuUrl] [varchar](100) NULL,
[F_ParentID] [int] NULL,
[F_MenuCode] [varchar](10) NULL,
[F_Order] [int] NULL,
[F_IsMenu] [bit] NULL,
[F_IsWindow] [bit] NULL,
CONSTRAINT [PK_T_4s_Menu] PRIMARY KEY CLUSTERED
(
[F_MenuID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO

存储步骤例如以下:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <CSDN_5653325>
-- Create date: <20130729>
-- Description: <Admin_Menu转为JSON类型>
-- =============================================
ALTER PROCEDURE [dbo].[pr_4sMenu2JSON] (@i_AdminID int,@i_GroupID int,@s_ReturnJson varchar(5000) OUTPUT)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @JSON varchar(5000);--要输出的JSON字符串 DECLARE @root_MenuCount int;--根菜单数量
DECLARE @root_MenuID int;--根菜单ID
DECLARE @root_MenuName nvarchar(50);--根菜单名称
DECLARE @root_MenuCode varchar(10);--根菜单编码 DECLARE @Return_Rules varchar(3000);--返回查询的权限 IF(@i_AdminID=-1 AND @i_GroupID=-1) --所有
BEGIN
SELECT @Return_Rules=',"Rules":"Null"';
END
ELSE
BEGIN
IF(@i_AdminID>-1)--指定4SID
BEGIN
SELECT @Return_Rules=',{'+[F_Rules]+'}' FROM [T_4s] WHERE [F_ID]=@i_AdminID
END
ELSE --指定用户ID
BEGIN
SELECT @Return_Rules=',{'+[F_UserPower]+'}' FROM [T_4s_Users] WHERE [ID]=@i_GroupID
END
END SELECT @root_MenuCount=COUNT(0) FROM T_4s_Menu WHERE F_ParentID=1 AND F_MenuID<>1
SELECT @JSON='[{"count":'+CONVERT(varchar(10),@root_MenuCount)+',"list":[';
DECLARE @i int=1;--根菜单循环计数器,用来推断是不是读取最后一条记录 DECLARE cur_Root CURSOR FOR
SELECT [F_MenuID],[F_MenuName],[F_MenuCode] FROM T_4s_Menu WHERE F_ParentID=1 AND F_MenuID<>1 order by F_Order asc
OPEN cur_Root
FETCH NEXT FROM cur_Root INTO @root_MenuID, @root_MenuName,@root_MenuCode
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @MenuID int;--子菜单ID
DECLARE @MenuName nvarchar(50);--子菜单名称
DECLARE @MenuCode varchar(10);--子菜单编码
DECLARE @MenuUrl varchar(100);--子菜单URL
DECLARE @IsMenu bit; --子菜单是否显示
DECLARE @MenuCount int;--子菜单数量
DECLARE @j int=1;--子菜单循环计数器,用来推断是否是最后一条记录 SELECT @MenuCount=COUNT(0) FROM T_4s_Menu WHERE F_ParentID=@root_MenuID
DECLARE cur_Menu CURSOR FOR
SELECT [F_MenuID],[F_MenuName],[F_MenuCode],[F_MenuUrl],[F_IsMenu] FROM T_4s_Menu WHERE F_ParentID=@root_MenuID order by F_Order asc
OPEN cur_Menu
FETCH NEXT FROM cur_Menu INTO @MenuID, @MenuName,@MenuCode,@MenuUrl,@IsMenu IF @i=@root_MenuCount --最后一行数据
BEGIN
SELECT @JSON+='{"MenuID":'+CONVERT(varchar(10),@root_MenuID)+',"MenuName":"'+@root_MenuName+'","MenuCode":"'+@root_MenuCode+'","MenuCount":'+CONVERT(varchar(10),@MenuCount)+',"MenuList":[' WHILE @@FETCH_STATUS = 0
BEGIN
IF @j=@MenuCount--最后一条记录
BEGIN
SELECT @JSON+='{"MenuID":'+CONVERT(varchar(10),@MenuID)+',"MenuName":"'+@MenuName+'","MenuCode":"'+@MenuCode+'","MenuUrl":"'+@MenuUrl+'","IsMenu":"'+convert(varchar(8),@IsMenu)+'"'
SELECT @JSON+='}]'
END
ELSE
BEGIN
SELECT @JSON+='{"MenuID":'+CONVERT(varchar(10),@MenuID)+',"MenuName":"'+@MenuName+'","MenuCode":"'+@MenuCode+'","MenuUrl":"'+@MenuUrl+'","IsMenu":"'+convert(varchar(8),@IsMenu)+'"'
SELECT @JSON+='},'
SELECT @j+=1;
END
FETCH NEXT FROM cur_Menu INTO @MenuID, @MenuName,@MenuCode,@MenuUrl,@IsMenu
END SELECT @JSON+='}'
END
ELSE
BEGIN
SELECT @JSON+='{"MenuID":'+CONVERT(varchar(10),@root_MenuID)+',"MenuName":"'+@root_MenuName+'","MenuCode":"'+@root_MenuCode+'","MenuCount":'+CONVERT(varchar(10),@MenuCount)+',"MenuList":[' WHILE @@FETCH_STATUS = 0
BEGIN
IF @j=@MenuCount--最后一条记录
BEGIN
SELECT @JSON+='{"MenuID":'+CONVERT(varchar(10),@MenuID)+',"MenuName":"'+@MenuName+'","MenuCode":"'+@MenuCode+'","MenuUrl":"'+@MenuUrl+'","IsMenu":"'+convert(varchar(8),@IsMenu)+'"'
SELECT @JSON+='}]'
END
ELSE
BEGIN
SELECT @JSON+='{"MenuID":'+CONVERT(varchar(10),@MenuID)+',"MenuName":"'+@MenuName+'","MenuCode":"'+@MenuCode+'","MenuUrl":"'+@MenuUrl+'","IsMenu":"'+convert(varchar(8),@IsMenu)+'"'
SELECT @JSON+='},'
SELECT @j+=1;
END
FETCH NEXT FROM cur_Menu INTO @MenuID, @MenuName,@MenuCode,@MenuUrl ,@IsMenu
END SELECT @JSON+='},'
SELECT @i+=1;
END CLOSE cur_Menu
DEALLOCATE cur_Menu
FETCH NEXT FROM cur_Root INTO @root_MenuID, @root_MenuName,@root_MenuCode
END CLOSE cur_Root
DEALLOCATE cur_Root
SELECT @JSON+=']'+@Return_Rules+'}]';
SELECT @s_ReturnJson= @JSON
END

结果例如以下:

[{"count":7,"list":[{"MenuID":2,"MenuName":"信息管理","MenuCode":"NS","MenuCount":2,"MenuList":[{"MenuID":3,"MenuName":"信息公布","MenuCode":"NS_Edit","MenuUrl":"Manage/News/NewsEdit.aspx","IsMenu":"1"},{"MenuID":17,"MenuName":"新闻列表","MenuCode":"NS_NL","MenuUrl":"Manage/News/NewsList.aspx","IsMenu":"1"}]},{"MenuID":4,"MenuName":"新车管理","MenuCode":"4C","MenuCount":2,"MenuList":[{"MenuID":5,"MenuName":"车辆报价","MenuCode":"4C_PC","MenuUrl":"Manage/Cars/CarPrice.aspx","IsMenu":"1"},{"MenuID":16,"MenuName":"所售车型","MenuCode":"4C_CL","MenuUrl":"Manage/Cars/SellCarList.aspx","IsMenu":"1"}]},{"MenuID":6,"MenuName":"留言咨询","MenuCode":"CM","MenuCount":4,"MenuList":[{"MenuID":7,"MenuName":"试乘试驾","MenuCode":"CM_App","MenuUrl":"Manage/Application/ApplicationList.aspx","IsMenu":"1"},{"MenuID":8,"MenuName":"底价咨询","MenuCode":"CM_zxdj","MenuUrl":"Manage/Comment/zxdj.aspx","IsMenu":"0"},{"MenuID":9,"MenuName":"客户留言","MenuCode":"CM_leave","MenuUrl":"Manage/LeaveMessage/LeaveMessage_List.aspx","IsMenu":"1"},{"MenuID":20,"MenuName":"站内消息","MenuCode":"CM_Mes","MenuUrl":"Manage/Message/Message_List.aspx","IsMenu":"0"}]},{"MenuID":26,"MenuName":"二手车管理","MenuCode":"2S","MenuCount":8,"MenuList":[{"MenuID":31,"MenuName":"二手车列表","MenuCode":"2S_List","MenuUrl":"Manage/2Shou/2CarList.aspx","IsMenu":"1"},{"MenuID":30,"MenuName":"二手车加入","MenuCode":"2S_ADD","MenuUrl":"Manage/2Shou/2CarAdd.aspx","IsMenu":"1"},{"MenuID":28,"MenuName":"已售车辆","MenuCode":"2S_Selled","MenuUrl":"Manage/2Shou/selled.aspx","IsMenu":"0"},{"MenuID":27,"MenuName":"竞价平台","MenuCode":"2S_P","MenuUrl":"Manage/2Shou/p.aspx","IsMenu":"1"},{"MenuID":29,"MenuName":"二手车改动","MenuCode":"2S_Edit","MenuUrl":"Manage/2Shou/2CarEdit.aspx","IsMenu":"0"},{"MenuID":38,"MenuName":"我的拍卖","MenuCode":"2S_MyP","MenuUrl":"Manage/2Shou/mysell.aspx","IsMenu":"0"},{"MenuID":39,"MenuName":"竞拍终端页","MenuCode":"2S_PZD","MenuUrl":"Manage/2Shou/2CarView.aspx","IsMenu":"0"},{"MenuID":40,"MenuName":"我的竞拍","MenuCode":"2S_MYPP","MenuUrl":"Manage/2Shou/my_p.aspx","IsMenu":"1"}]},{"MenuID":34,"MenuName":"团购管理","MenuCode":"4s_Tuan","MenuCount":4,"MenuList":[{"MenuID":35,"MenuName":"加入团购","MenuCode":"4s_AddTuan","MenuUrl":"Manage/tuan/tuanadd.aspx","IsMenu":"1"},{"MenuID":37,"MenuName":"团购上传","MenuCode":"Tuan_Up","MenuUrl":"Manage/tuan/tuanpicupload.aspx","IsMenu":"0"},{"MenuID":42,"MenuName":"团购列表","MenuCode":"4s_TuanLis","MenuUrl":"Manage/tuan/tuanList.aspx","IsMenu":"1"},{"MenuID":43,"MenuName":"參加团购人员","MenuCode":"4s_tuanPeo","MenuUrl":"Manage/tuan/tuanListPeople.aspx","IsMenu":"0"}]},{"MenuID":41,"MenuName":"推广功能","MenuCode":"TG","MenuCount":1,"MenuList":[{"MenuID":15,"MenuName":"我要推广","MenuCode":"TG_Push","MenuUrl":"Manage/Extend/MyExtend.aspx","IsMenu":"1"}]},{"MenuID":10,"MenuName":"系统功能","MenuCode":"CF","MenuCount":6,"MenuList":[{"MenuID":11,"MenuName":"我的信息","MenuCode":"CF_ME","MenuUrl":"Manage/mydefault.aspx","IsMenu":"0"},{"MenuID":12,"MenuName":"系统模板","MenuCode":"CF_MD","MenuUrl":"Manage/Model/MyModel.aspx","IsMenu":"0"},{"MenuID":13,"MenuName":"员工管理","MenuCode":"CF_USERS","MenuUrl":"Manage/Users/MyUser.aspx","IsMenu":"1"},{"MenuID":14,"MenuName":"我要充值","MenuCode":"CF_Pay","MenuUrl":"Manage/Pay/Pay.aspx","IsMenu":"0"},{"MenuID":33,"MenuName":"4s信息管理","MenuCode":"CF_XXGL","MenuUrl":"Manage/edit4sInfo.aspx","IsMenu":"1"},{"MenuID":36,"MenuName":"系统菜单","MenuCode":"CF_Menu","MenuUrl":"Manage/Menu/Default.aspx","IsMenu":"0"}]}],"Rules":"Null"}]

然后前台就能够通过JS决心打造一个菜单。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

存储过程和输出分辨率表菜单JSON格式字符串的更多相关文章

  1. [js开源组件开发]query组件,获取url参数和form表单json格式

    query组件,获取url参数和form表单json格式 距离上次的组件[js开源组件开发]ajax分页组件一转眼过去了近二十天,或许我一周一组件的承诺有了质疑声,但其实我一直在做,只是没人看到……, ...

  2. List转换成json格式字符串,json格式字符串转换成list

    一.List转换成json字符串 这个比较简单,导入gson-x.x.jar, List<User> users = new ArrayList<User>(); Gson g ...

  3. json格式字符串与java.util.Map的互转(借助于jackson.jar)

    package com.test.javaAPI.json; /** * json工具类 * * @author Wei * @time 2016年10月2日 下午4:25:25 */ public ...

  4. 关于json格式字符串解析并用mybatis存入数据库

    园子里面找了很多关于json解析后存入数据库的方法,不是太乱,就是没有写完,我下面的主题代码多是受下面两位的启发,请按顺序查看 http://www.cnblogs.com/tian830937/p/ ...

  5. js实现配置菜品规格时,向后台传一个json格式字符串

    由于本公司做的是订餐平台,那么在上传菜品时,需要配置菜品规格,比如份量(大中小),味道(猛辣,中辣,微辣) 由于这些数据,在表的设计时 没有保存到菜品表,那么在点击保存菜品数据时,配置规格这块数据,我 ...

  6. WebApi返回Json格式字符串

    WebApi返回json格式字符串, 在网上能找到好几种方法, 其中有三种普遍的方法, 但是感觉都不怎么好. 先贴一下, 网上给的常用方法吧. 方法一:(改配置法) 找到Global.asax文件,在 ...

  7. iOS:JSON格式字符串转字典,字典转JSON格式字符串

    在iOS开发中,和服务器交互中,经常用到字典和JSON格式字符串相互转换. 代码如下: 1.JSON格式字符串转字典 + (NSDictionary *)dictionaryWithJsonStrin ...

  8. JSon_零基础_001_将布尔类型数组转换为JSon格式字符串,返回给界面

    将布尔类型数组转换为JSon格式字符串,返回给界面 需要导入包: 编写bean: package com.west.webcourse.po; /** * 第01步:编写bean类, * 下一步com ...

  9. FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换

    fastJson对于json格式字符串的解析主要用到了一下三个类: JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换. JSONObject:fas ...

随机推荐

  1. 用strace排查故障的5种简单方法(每日一译)

    原文链接:5 simple ways to troubleshoot using Strace 我很意外大部分人都不知道如何使用strace.strace一直是我的首选debug工具,因为它非常的有效 ...

  2. 12行Python暴力爬《黑豹》豆瓣短评

    作者:黄嘉锋 来源:https://www.jianshu.com/p/ea0b56e3bd86 草长莺飞,转眼间又到了三月"爬虫月".这时往往不少童鞋写论文苦于数据获取艰难,辗转 ...

  3. jquery画图插件jPainter

    jquery画图插件jPainter 一.总结 一句话总结:四年前的项目,四年无更新,不好用. 二.基于HTML5 Canvas和jQuery 的画图工具的实现 简介 HTML5 提供了强大的Canv ...

  4. js进阶正则表达式方括号(方括号作用)(js正则是在双正斜杠之中:/[a-z]/g)

    js进阶正则表达式方括号(方括号作用)(js正则是在双正斜杠之中:/[a-z]/g) 一.总结 方括号:范围 圆括号:选 大括号:数量 1.js正则是在双正斜杠之中: var reg2=/[a-z]/ ...

  5. javascript数组全排列,数组元素所有组合

    function permute(input) { var permArr = [], usedChars = []; function main(input){ var i, ch; for (i ...

  6. 如何查看一个网页特定效果的js代码(动画效果可js和css)(页面可以看到js的源代码)

    如何查看一个网页特定效果的js代码(动画效果可js和css)(页面可以看到js的源代码) 一.总结 1.动画效果可能是 CSS 实现的,也可能是 JS 实现的. 2.直接Chrome的F12调试即可, ...

  7. [ExtJS5学习笔记]第九节 Extjs5的mvc与mvvm框架结构简单介绍

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/38537431 本文作者:sushengmiyan ------------------ ...

  8. [NPM] Use custom config settings in your npm scripts

    In addition to package.json level variables (such as name and version), you can have custom conf set ...

  9. 移动CMPP3.0接口

    前段时间准备上线期,同事接了个联调CMPP3.0短信接口的任务,但是一直不成功,抽时间给解决了一下,记录下其中几个要点: 1.短信网关厂家需要提供参数: #网关IP地址 ismgIp=1.1.1.1# ...

  10. JVM源码分析之System.currentTimeMillis及nanoTime原理详解

    JDK7和JDK8下的System.nanoTime()输出完全不一样,而且差距还非常大,是不是两个版本里的实现不一样,之前我也没注意过这个细节,觉得非常奇怪,于是自己也在本地mac机器上马上测试了一 ...