Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

ASP.NET使用Newtonsoft.Json反解析带子集的JSON字符串

$
0
0

话不多说,直接上代码,不复杂。

代码是解析的一级菜单和二级菜单,如果有三级以上的菜单就可以考虑做成递归形式的。

        string JsonData = "{\"count\":1,\"list\":[{\"MenuID\":2,\"MenuName\":\"NS\",\"MenuCount\":2,\"MenuList\":[{\"MenuID\":3,\"MenuName\":\"NS_Edit\",\"MenuRules\":\"1000\"},{\"MenuID\":22,\"MenuName\":\"NS_NL\",\"MenuRules\":\"1000\"}]}]}";

        var MenuList = JsonConvert.DeserializeObject<MenuList>(JsonData);

        foreach (var key in MenuList.list)
        {
            Response.Write(key.ToString()+"<br />");

            var Menu = JsonConvert.DeserializeObject<Menu>(key.ToString());
            Response.Write("MenuID:" + Menu.MenuID + "<br />");
            Response.Write("MenuName:" + Menu.MenuName + "<br />");
            if (Menu.MenuCount == 0)
            {
                Response.Write("MenuRules:" + Menu.MenuRules + "<br />");
            }
            else
            {
                Response.Write("MenuCount:" + Menu.MenuCount + "<br />");
                for (int i = 0; i < Menu.MenuCount; i++)                {
                    var s_MenuList = JsonConvert.DeserializeObject<Menu>(Menu.MenuList[i].ToString());
                    Response.Write("MenuID:" + s_MenuList.MenuID + "<br />");
                    Response.Write("MenuName:" + s_MenuList.MenuName + "<br />");
                    Response.Write("MenuRules:" + s_MenuList.MenuRules + "<br />");
                }
            }           
        }
    }
    public class MenuList
    {
        public int count;
        public ArrayList list;
    }

    public class Menu
    {
        public int MenuID;
        public string MenuName;
        public int MenuCount;
        public ArrayList MenuList;
        public string MenuRules;

    }


 

作者:5653325 发表于2013-7-30 14:49:59 原文链接
阅读:22 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>