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

                        }

                    });

 

 

你可能感兴趣的:(asp.net)