代码改变世界

javascript序列化json 第二篇

2013-07-08 15:40  sql_manage  阅读(271)  评论(0编辑  收藏  举报

这一节主要是记录父子关系的JSON生成,废话不多说,直接上代码。

 public partial class _default : System.Web.UI.Page
    {
        public static string meunString = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
           //meunString= getMeunData()+";";
        }

        public string getMeunData()
        {
            List<FristMeun> firstMeunList = new List<FristMeun>();
            for (int i = 0; i < 3; i++)
            {
                string _name = "菜单" + i.ToString();
                string _icon = "GlobalStyle/images/ui-icon_home.gif";
                List<SecondMeun> secondMeunList = new List<SecondMeun>();
                for (int s = 0; s < 10; s++)
                {
                    SecondMeun secondMeun = new SecondMeun {
                        label = "代理管理",
                        css = "edit",
                        target = "mainFrame",
                        url = "Agent/AgentDelegate.aspx"
                    };
                    secondMeunList.Add(secondMeun);
                }
                FristMeun fristMeun = new FristMeun {
                    name = _name,
                    icon = _icon,
                    items=secondMeunList
                };
                firstMeunList.Add(fristMeun);
                
            }
            if (firstMeunList.Count > 0)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string resultString = serializer.Serialize(firstMeunList);
                return resultString+";";
            }
            else
            {
                return "";
            }
        }
    }

    public class FristMeun
    {
        public string name { get; set; }
        public string icon { get; set; }
        public List<SecondMeun> items { get; set; }
    }
    public class SecondMeun
    {
        public string label { get; set; }
        public string css { get; set; }
        public string target { get; set; }
        public string url { get; set; }
    }

  aspx页面调用后台方法的写法:var menuSource =<%= getMeunData()%>