存储过程和输出分辨率表菜单JSON格式字符串
表的结构,如以下:
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格式字符串的更多相关文章
- [js开源组件开发]query组件,获取url参数和form表单json格式
query组件,获取url参数和form表单json格式 距离上次的组件[js开源组件开发]ajax分页组件一转眼过去了近二十天,或许我一周一组件的承诺有了质疑声,但其实我一直在做,只是没人看到……, ...
- List转换成json格式字符串,json格式字符串转换成list
一.List转换成json字符串 这个比较简单,导入gson-x.x.jar, List<User> users = new ArrayList<User>(); Gson g ...
- json格式字符串与java.util.Map的互转(借助于jackson.jar)
package com.test.javaAPI.json; /** * json工具类 * * @author Wei * @time 2016年10月2日 下午4:25:25 */ public ...
- 关于json格式字符串解析并用mybatis存入数据库
园子里面找了很多关于json解析后存入数据库的方法,不是太乱,就是没有写完,我下面的主题代码多是受下面两位的启发,请按顺序查看 http://www.cnblogs.com/tian830937/p/ ...
- js实现配置菜品规格时,向后台传一个json格式字符串
由于本公司做的是订餐平台,那么在上传菜品时,需要配置菜品规格,比如份量(大中小),味道(猛辣,中辣,微辣) 由于这些数据,在表的设计时 没有保存到菜品表,那么在点击保存菜品数据时,配置规格这块数据,我 ...
- WebApi返回Json格式字符串
WebApi返回json格式字符串, 在网上能找到好几种方法, 其中有三种普遍的方法, 但是感觉都不怎么好. 先贴一下, 网上给的常用方法吧. 方法一:(改配置法) 找到Global.asax文件,在 ...
- iOS:JSON格式字符串转字典,字典转JSON格式字符串
在iOS开发中,和服务器交互中,经常用到字典和JSON格式字符串相互转换. 代码如下: 1.JSON格式字符串转字典 + (NSDictionary *)dictionaryWithJsonStrin ...
- JSon_零基础_001_将布尔类型数组转换为JSon格式字符串,返回给界面
将布尔类型数组转换为JSon格式字符串,返回给界面 需要导入包: 编写bean: package com.west.webcourse.po; /** * 第01步:编写bean类, * 下一步com ...
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
fastJson对于json格式字符串的解析主要用到了一下三个类: JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换. JSONObject:fas ...
随机推荐
- mysql查询字段所在表
use information_schema;select * from columns where column_name='字段名' ;
- Jcaptca 图片
http://www.oschina.net/code/snippet_89964_6009 http://blog.csdn.net/tender001/article/details/846969 ...
- mac nginx php-fpm
再一次被困在一个傻问题.由于我居然怀疑是不是mac本身就和centos的安装不一样.在一次次地排错后,最终发现.原来是我的nginx.conf的一行配置少写了一个字母.最后多亏用ls检查来定位到这个错 ...
- 使用Nexus搭建Maven仓库
1.目的 通过建立自己的私服,能够减少中央仓库负荷.节省外网宽带.加速maven构建.自己部署构件等,从而高效的使用maven,nexus是当前流行的Maven仓库管理软件. 2.下载nexus 2. ...
- Windows批处理(cmd/bat)常用命令
Windows批处理(cmd/bat)常用命令 一.总结 一句话总结: 1.批量处理图片的方式? PS批处理是基于强大的图片编辑软件Photoshop的,用来批量处理图片的脚本: 2.大量的重复的操作 ...
- Windows7系统下安装OpenSSL攻略
Windows7系统下安装OpenSSL攻略 http://blog.chinaunix.net/uid-20479991-id-216269.html http://my.oschina.net/s ...
- 深度学习框架 —— tflearn 的学习
1. tflearn.data_utils from tflearn.data_utils import to_categorical one_hot 编码: 第一个参数为属性列,第二个参数接受类别个 ...
- cellForRowAtIndexPath 设置图片
#import "UIImageView+MJWebCache.h" #import "MJPhotoBrowser.h" #import "MJPh ...
- 小强的HTML5移动开发之路(24)—— PhoneGap Android开发环境搭建
有关JDK及Android开发环境的搭建请看我前面的博文:http://blog.csdn.net/dawanganban/article/details/9748497 一.下载PhoneGap 下 ...
- 一起学Python:协程
一:协程-yield 协程,又称微线程,纤程.英文名Coroutine. 协程是啥 协程是python个中另外一种实现多任务的方式,只不过比线程更小占用更小执行单元(理解为需要的资源). 为啥说它是一 ...