码迷,mamicode.com
首页 > Web开发 > 详细

jQuery读取JSON总结

时间:2014-12-08 17:32:34      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:des   style   io   ar   color   os   使用   sp   for   

1.jQuery 部分

<script src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function() {
        //alert("json");
        var user = { "UserID": 11, "Name": "Truly", "Email": "zhuleipro@hotmail.com" };
        alert(user.UserID);
        alert(user["Name"]);
        //alert(user);
    }); //读取简单对象 
    $(document).ready(function() {
        var user = { "UserID": 11, "Name": { "FristName": "Truly", "LastName": "Zhu" }, "Emali": "JerryTienCN@126.vom" };
        alert(user.Name.FristName);
        alert(user["Name"]["FristName"]);
    });
    $(document).ready(function() {
        var userList = [
        { "UserID": 11, "Name": { "FirstName": "Jerry", "LastName": "Tian" }, "Email": "address1" },
        { "UserID": 12, "Name": { "FirstName": "Jerry1", "LastName": "Tian" }, "Email": "address2" },
        { "UserID": 13, "Name": { "FirstName": "Jerry2", "LastName": "Tian" }, "Email": "address3" },
            ];
        alert(userList[0].Name.FirstName);
        alert(userList.length); //读取复杂对象 
    });
</script>

 

2.JSON部分

Json对象从后台传输可以使用WebServie asmx aspx多种方式

Json对象包含在[ ]中

Json数据变量名要用 ""

Json数据需要用{ }分割

前台调用代码如下 :

  <script type="text/javascript">
        $(document).ready(function() {
            $.ajax({
                url: "NavigateTree.asmx/GetJson",
                type: "POST",
                dataType: "json",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                success: function(json) {
                    alert(json.d);
                    var data = eval(‘(‘ + json.d + ‘)‘);
                    alert(data);
                    alert(data.length);
                    alert(data[0].showcheck);
                },
                error: function(x, e) {
                    alert(x.responsetText);
                    alert("Error");
                },
                complete: function(x) {
                    //alert(x.responseText);
                }
            });
        });
    </script>

后台代码如下

        [WebMethod]
        public static string GetJson()
        {
            string json = "[";
            List<tbNavigationTree> t = NaviagetTreeDlinqDAL.returnParetnTree();
            foreach (tbNavigationTree model in t)
            {
                if (model != t[t.Count - 1])
                {
                    json += GetJsonByModel(model) + ",";
                }
                else
                {
                    json += GetJsonByModel(model);
                }
            }
            json += "]";
            json=json.Replace("‘","/"");
            return json;
        }
        public static string GetJsonByModel(tbNavigationTree t)
        {
            string json = "";
            bool flag = NaviagetTreeDlinqDAL.isHavingChild(t.ID);
            json = "{"
                + "‘id‘:‘" + t.ID + "‘,"
                + "‘text‘:‘" + t.ModuleName + "‘,"
                + "‘‘value‘:‘" + t.ID + "‘,"
                + "‘‘showcheck‘:true,"
                + "‘checkstate‘:‘0‘,"
                + "‘hasChildren‘:" + flag.ToString().ToLower() + ","
                + "‘isexpand‘:false,"
                + "‘ChildNodes‘:"; //ChildNodes C一定大写
            if (!flag)
            {
                json += "null,";
                json += "complete‘‘;false}";
            }
            else
            {
                json += "[";
                List<tbNavigationTree> list = NaviagetTreeDlinqDAL.getChild(t.ID);
                foreach(tbNavigationTree tree in list)
                {
                    if (tree != list[list.Count - 1]) 
                    {
                        json += GetJsonByModel(tree) + ",";
                    }
                    else
                    {
                        json+=GetJsonByModel(tree);
                    }
                }
                json+="],‘complete‘:true}";
            }
            return json;
        }

jQuery读取JSON总结

标签:des   style   io   ar   color   os   使用   sp   for   

原文地址:http://www.cnblogs.com/mrjie/p/4151274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!