zoukankan      html  css  js  c++  java
  • 获取json数据

    获取json数据

    1.获取DataSet
    2.转成DataTable
    3.
    public static string DataTableToJson(DataTable dt)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            //jsonBuilder.Append("{");  //输出的要求不要'}'
            //jsonBuilder.Append(dt.TableName);
            jsonBuilder.Append("[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString());
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]");
            //jsonBuilder.Append("}");  //输出的要求不要'}'
            return jsonBuilder.ToString();
        }
    4.把Json字串Response出来
    5.获取Json字串

    function load()     //页面加载
            {           
                $.ajax({
                    type:"post",
                    url:"../Handler/LinkManHandler.ashx",//从哪获取Json
                    data:"action=getLinkMan&id="+comId,//Ajax传递的参数
                   
                    beforeSend:function(){
                        //获取之前显示 loading 或 正在加载
                    },
                   
                    success:function(mess)
                    {
                        $("#divImage").hide();
                        if(mess!="]")// 如果获取的数据不为空
                        {
                             var type=eval(mess);
                             var str="<a>首页</a>"
                             for(var i=0;i<type.length;i++)
                             {
                                //操作type 获取东西
                                str+="<a>"+type[i].数据库字段名+"</a>
                             }
                             $("#divLinkMan").html(str);//把str嵌入div divLinkMan 中
                         }
                         else
                         {
                            var notfind="<h2>未找到数据<a href='../Main.htm' target='_parent'>返回</a></h2>"
                            $("#divLinkMan").html(notfind);
                         }
                        
                    },
                   
                    complete:function()
                    {
                        //所有的都执行完后的方法
                    }
                });
            }

  • 相关阅读:
    (.*?)懒惰正则
    re.S
    import和from import陷阱一
    import和from import陷阱二
    Linux中查看文件编码
    环境部署策略
    简便删除已经存在的oracle数据库用户UPAY3LINGXI_YS
    ssh连接linux服务器只显示-bash-4.1#不显示路径解决方法
    ubuntu安装出现"删除initramfs-tools时出错",subprocess installed post-installation script returned error exit status 1
    python下载链接内容
  • 原文地址:https://www.cnblogs.com/sgivee/p/1775915.html
Copyright © 2011-2022 走看看