博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jQuery读取JSON总结
阅读量:5149 次
发布时间:2019-06-13

本文共 2634 字,大约阅读时间需要 8 分钟。

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;
        }

转载于:https://www.cnblogs.com/luckk/p/4151274.html

你可能感兴趣的文章
LeetCode 74. Search a 2D Matrix(搜索二维矩阵)
查看>>
利用SignalR来同步更新Winfrom
查看>>
反射机制
查看>>
CocoaPod
查看>>
BZOJ 1251: 序列终结者 [splay]
查看>>
5G边缘网络虚拟化的利器:vCPE和SD-WAN
查看>>
MATLAB基础入门笔记
查看>>
【UVA】434-Matty&#39;s Blocks
查看>>
Android开发技术周报 Issue#80
查看>>
hadoop2.2.0+hive-0.10.0完全分布式安装方法
查看>>
django知识点总结
查看>>
C++ STL stack、queue和vector的使用
查看>>
OAuth2 .net MVC实现获取token
查看>>
java中XML操作:xml与string互转、读取XML文档节点及对XML节点增删改查
查看>>
使用Reporting Services时遇到的小问题
查看>>
约瑟夫问题
查看>>
Arduino 报错总结
查看>>
树莓派Android Things物联网开发:树莓派GPIO引脚图
查看>>
矩阵快速幂---BestCoder Round#8 1002
查看>>
js兼容公用方法
查看>>