json数据交换的例了
生活随笔
收集整理的這篇文章主要介紹了
json数据交换的例了
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
我打算在我的個人網(wǎng)站中,取消傳統(tǒng)的xml與前臺傳遞數(shù)據(jù)。改用json數(shù)據(jù)格式
這是我前臺的一部分:
??var?option={
????????parameters:"",
????????method:"get",
????????onSuccess:function(transport){
?????????????rp=transport.responseText;
????????????var?json=eval('('?+rp+?')');
????for(var?i=0;i<json.DataTable.length;i++)
????{
????????taskMenu2.add(new?TaskMenuItem(json.DataTable[i].lifecol,"Image/friends.gif","parent.window.frames[1].location.href='Redirect.aspx?id="+json.DataTable[i].id+"'"));
???????
????}
????
????????},
????????onFailure:function(transport){
????????????alert(transport.responseText+"數(shù)據(jù)提交失敗,請檢查網(wǎng)絡(luò)或重試。");
????????}
????};
var?request=new?Ajax.Request("ajax.aspx",option);
這里的taskMenu2這個是我自已定義的javascript類。不用去管它:
下面是。ajax.aspx.cs頁面
????if?(!this.IsPostBack)
????????{?
????????????????SqlDataReader?sdr=SqlHelper.ExecuteReader(CommandType.Text,"select?*?from?readlife",null);
????????
??????????????DataTable?dt?=?DB.ConvertDataReaderToDataTable(sdr);
??????????????Response.Write(DB.DataTableToJSON(dt));
??????????????
????????
????????} 這里用到了一個方法:
在DB類中有一個靜態(tài)方法:
?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();
????}
這是我前臺的一部分:
??var?option={
????????parameters:"",
????????method:"get",
????????onSuccess:function(transport){
?????????????rp=transport.responseText;
????????????var?json=eval('('?+rp+?')');
????for(var?i=0;i<json.DataTable.length;i++)
????{
????????taskMenu2.add(new?TaskMenuItem(json.DataTable[i].lifecol,"Image/friends.gif","parent.window.frames[1].location.href='Redirect.aspx?id="+json.DataTable[i].id+"'"));
???????
????}
????
????????},
????????onFailure:function(transport){
????????????alert(transport.responseText+"數(shù)據(jù)提交失敗,請檢查網(wǎng)絡(luò)或重試。");
????????}
????};
var?request=new?Ajax.Request("ajax.aspx",option);
這里的taskMenu2這個是我自已定義的javascript類。不用去管它:
下面是。ajax.aspx.cs頁面
????if?(!this.IsPostBack)
????????{?
????????????????SqlDataReader?sdr=SqlHelper.ExecuteReader(CommandType.Text,"select?*?from?readlife",null);
????????
??????????????DataTable?dt?=?DB.ConvertDataReaderToDataTable(sdr);
??????????????Response.Write(DB.DataTableToJSON(dt));
??????????????
????????
????????} 這里用到了一個方法:
在DB類中有一個靜態(tài)方法:
?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();
????}
總結(jié)
以上是生活随笔為你收集整理的json数据交换的例了的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是VLAN
- 下一篇: ASP.NET基础教程-以查询字符串的方