dojo从asp.net中获取json数据

搞来有搞去终于有了个结果,主要是一开始犯了一些低级错误。

对于json不太了解的童鞋,可以看看这个:http://www.dreamdu.com/blog/2008/10/19/json_in_javascript/

这个例子中主要是从数据库中读取数据,转换成JSON格式,传递给前端,废话少说上代码:

复制代码
protected void Page_Load(object sender, EventArgs e)
        {
             string b = Request["callback"];


            string name = Context.Request["name"];
            //数据库操作
            SqlConnection thisConnection = new SqlConnection("Data Source=localhost;Initial Catalog=commodities; uid=sa;Password=123");
            string command = "select * from InterestStore where UserID = " + name;
            SqlDataAdapter thisAdapter = new SqlDataAdapter(command, thisConnection);
            SqlCommandBuilder thisCommandBuilder = new SqlCommandBuilder(thisAdapter);
            DataSet thisDataSet = new DataSet();
            thisAdapter.Fill(thisDataSet, "IStore");
            //将数据表转换成JSON数据
            string ss = GetJson(thisDataSet, "IStore");
           //返回到前端
            Response.Write(ss);
            Response.End();//Response.End把前面的内容都输出了,阻止了后面html相关内容的输出
        }
复制代码

下面是将数据转化成JSON代码(改了好长时间,哈哈)

复制代码
/// <summary>
        /// 获取Json数据
        /// </summary>
        /// <param name="dSet">数据集</param>
        /// <param name="strTableName">表名</param>
        /// <returns></returns>
        private string GetJson(DataSet dSet, string strTableName)
        {
            StringBuilder sBuilder = new StringBuilder();

            sBuilder.Append("{");
            //sBuilder.Append(" " + strTableName + ":{");
            sBuilder.Append("\"" + strTableName +"\":[");

            try {
                for (int i = 0; i < dSet.Tables[strTableName].Rows.Count; i++)
                {
                    sBuilder.Append("{");
                    for (int j = 0; j < dSet.Tables[strTableName].Columns.Count; j++)
                    {
                        sBuilder.AppendFormat("\"{0}\":\"{1}\",", dSet.Tables[strTableName].Columns[j].ColumnName, dSet.Tables[strTableName].Rows[i][j].ToString());
                    }
                    sBuilder.Remove(sBuilder.ToString().LastIndexOf(','), 1);
                    sBuilder.Append("},");
                }
                sBuilder.Remove(sBuilder.ToString().LastIndexOf(','), 1);

                sBuilder.Append("]");
                sBuilder.Append("}");
                //sBuilder.Append(" };");
                return sBuilder.ToString();
            }
            catch (Exception es){
                throw new Exception(es.Message);
            }
        }
复制代码

前台返回的数据:

{"IStore":[{"SOIID":"1 ","UserID":"001 ","StoreID":"47 ","Attention":"10"},{"SOIID":"2 ","UserID":"001 ","StoreID":"77 ","Attention":"8"},{"SOIID":"3 ","UserID":"001 ","StoreID":"81 ","Attention":"8"},{"SOIID":"4 ","UserID":"001 ","StoreID":"42 ","Attention":"7"},{"SOIID":"5 ","UserID":"001 ","StoreID":"81 ","Attention":"6"},{"SOIID":"6 ","UserID":"001 ","StoreID":"114 ","Attention":"6"},{"SOIID":"7 ","UserID":"001 ","StoreID":"106 ","Attention":"8"},{"SOIID":"8 ","UserID":"001 ","StoreID":"116 ","Attention":"6"}]}

前台执行ajax调用的代码

复制代码
 xhr.get({
                        //请求页面
                        url: "WebForm3.aspx",
                        //参数
                        content: { name: sname },
                        //数据格式
                        handleAs: "json",
                        //当执行成功时调用的方法
                        load: function (newContent) {
                            try {
                                
                                var str = newContent;
                                console.log(str);
                                console.log("ddddddddddddddddddd");
                                console.log(dojo.toJson(str));
                            }
                            catch (err)
                            { console.log(err); }
                            //dom.byId("txtSuggestion").value = people.programmers[0].lastName;
                            dom.byId("txtSuggestion").value = newContent.IStore[2].StoreID;
                        },
                        //失败时调用的方法
                        error: function (err) {
                            //alert("error");
                            alert(err);
                        }
                    });
复制代码

 

 

posted @   木的树  阅读(1156)  评论(2编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示