jQuery读取JSON总结

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": "[email protected]" };
        alert(user.UserID);
        alert(user["Name"]);
        //alert(user);
    }); //读取简单对象 
    $(document).ready(function() {
        var user = { "UserID": 11, "Name": { "FristName": "Truly", "LastName": "Zhu" }, "Emali": "[email protected]" };
        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;
        }

时间: 2024-11-06 10:32:41

jQuery读取JSON总结的相关文章

jQuery读取json文件

转 http://www.jb51.net/article/36678.htm 1.userInfo.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml&qu

jQuery读取json文件,实现省市区/县(国标)三级联动

position:static(静态定位) 当position属性定义为static时,可以将元素定义为静态位置,所谓静态位置就是各个元素在HTML文档流中应有的位置 podisition定位问题.所以当没有定义position属性时,并不说明该元素没有自己的位置,它会遵循默认显示为静态位置,在静态定位状态下无法通过坐标值(top,left,right,bottom)来改变它的位置. position:absolute(绝对定位) 当position属性定义为absolute时,元素会脱离文档流

通过jquery,从json中读取数据追加到html中

1.下载安装jquery 可通过下面的方法引入在线版本的js: <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> 参考安装文档:http://www.runoob.com/jquery/jquery-install.html 2.准备一个json格式的文件,后缀可以不是.json 例如下面是result.json的格式 {    "title&q

使用Jquery解析Json基础知识

前言 在WEB数据传输过程中,json是以文本,即字符串的轻量级形式传递的,而客户端一般用JS操作的是接收到的JSON对象,所以,JSON对象和JSON字符串之间的相互转换.JSON数据的解析是关键. 先明确2个概念例如: JSON字符串: var str1 = '{ "name": "deyuyi", "sex": "man" }'; JSON对象: var str2 = { "name": "

jqery ajax读取json文件

json文件数据 [ {"name":"哈哈··","email":"邮箱01","gender":"男","hobby":["上网","打球"]}, {"name":"呵呵··","email":"邮箱02","gender":

jQuery获取JSON格式数据方法

getJSON方法: jQuery.getJSON(url,data,success(data,status,xhr)) $("button").click(function(){ $.getJSON("demo_ajax_json.js",function(result_data){ $.each(result_data, function(index, name_value){ $("div").append(name_value + &qu

使用Jquery解析Json基础知识(转)

在WEB数据传输过程中,json是以文本,即字符串的轻量级形式传递的,而客户端一般用JS操作的是接收到的JSON对象,所以,JSON对象和JSON字符串之间的相互转换.JSON数据的解析是关键. 先明确2个概念例如: JSON字符串: var str1 = '{ "name": "deyuyi", "sex": "man" }'; JSON对象: var str2 = { "name": "del

使用Jquery解析Json

利用原生JSON对象,将对象转为字符串 [javascript] view plaincopy var jsObj = {}; jsObj.testArray = [1,2,3,4,5];   jsObj.name = 'CSS3';   jsObj.date = '8 May, 2011';   var str = JSON.stringify(jsObj);   alert(str); 从JSON字符串转为对象 [javascript] view plaincopy var jsObj =

jQuery解析json详解

jQuery解析Json详解:http://www.cnblogs.com/madyina/p/3448518.html JSON简介及用法:http://bbs.html5cn.org/thread-87020-1-1.html 不建议使用eval()函数,因为eval()接受任意的字符串,并当作JavaScript代码来处理,这个机制已经有安全隐患了 var str='{ "name": "John" }'; var obj = eval  ('(' + str