首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

传入json字符串格式有关问题,请高手赐教

2013-01-26 
传入json字符串格式问题,请高手赐教aspx页面中的脚本如下: function selectAll() {var table$.ajax({url:

传入json字符串格式问题,请高手赐教
aspx页面中的脚本如下:
 function selectAll() {
               var table;
               $.ajax({
                   url: "1.ashx",
                   type: "post",
                   dataType: "json",
                   data: { action: "all" },
                   async:false ,
                   success: function (data) {
                       if (data) {
                           table = eval("("+data +")");
                       }
                   }
               });
                alert(table .Rows.length);
        }
1.ashx的部分代码如下:
 context.Response.ContentType = "text/plain";

            StringBuilder jsonStr = new StringBuilder();
            DataTable table = new SqlHelper().GetTable("select Name,Age,Job,Introduc from dbo.Member");
            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow row = table.Rows[i];
                string m = "{" + string.Format("name:'{0}',age:'{1}',job:'{2}',Introduc:'{3}'", row[0], row[1], row[2], row[3]) + "}";
                jsonStr.Append(m);
            }
            string finalJson = "{" + string.Format("Name:'table',Rows:'{0}'","["+ jsonStr.ToString()+"]") + "}";
            context.Response.Write(finalJson);
这个json字符串格式的字符串finalJson 传不会页面是如何回事啊?

另外在文本可视化工具中看到finalJson这个变量的值就是
{Name:'table',Rows:'[{name:'啦啦',age:'21',job:'软件开发',Introduc:'我一定会越来越强的'}{name:'haha',age:'21',job:'软件测试',Introduc:'我一定会越来越强的'}{name:'嘻嘻',age:'21',job:'美工',Introduc:'我一定会越来越强的'}{name:'haha',age:'21',job:'软件测试',Introduc:'我一定会越来越强的'}{name:'哟哟',age:'21',job:'Web UI',Introduc:'我一定会越来越强的'}{name:'mangran',age:'21',job:'架构设计',Introduc:'我一定会越来越强的'}]'}


[解决办法]
将你的数据结构在后台生命class:

 public class MyTableData
    {
        public string Name { get; set; }
        public List<MyRowData> Rows { get; set; }
    }

    public class MyRowData
    {
        //name:'{0}',age:'{1}',job:'{2}',Introduc
        public string name { get; set; }
        public string age { get; set; }
        public string job { get; set; }
        public string Introduc { get; set; }
    }

然后使用JavaScriptSerializer对象生成Json字符串。JavaScriptSerializer在System.Web.Extensions里面,namespace为System.Web.Script.Serialization
            
DataTable table = new SqlHelper().GetTable("select Name,Age,Job,Introduc from dbo.Member");
            MyTableData tableData = new MyTableData();
            tableData.Rows = new List<MyRowData>();

            for (int i = 0; i < table.Rows.Count; i++)
            {
                DataRow row = table.Rows[i];
                MyRowData rowData = new MyRowData();
                rowData.name = row[0].ToString();
                rowData.age = row[1].ToString();
                rowData.job = row[2].ToString();
                rowData.Introduc = row[3].ToString();
                tableData.Rows.Add(rowData);
            }

            string finalJson = new JavaScriptSerializer().Serialize(tableData);

[解决办法]
数组{}{}中间加逗号,
{"Name":"table","Rows":[{"name":"啦啦","age":"21","job":"软件开发","Introduc":"我一定会越来越强的"},{"name":"啦啦","age":"21","job":"软件开发","Introduc":"我一定会越来越强的"}]}

热点排行