今天使用了jquery提供的对json的操作的函数,感觉超级爽哦!下面就把代码贴出
js
js
1function getjson()
2 {
3 $("[id=ready]").remove();//返回id=ready的所有dom元素
4
5 $.ajax(
6 {
7 type:"get",
8 dataType:"json",
9 url:"jspage.aspx",
10 data:"id=1",
11 success:function(msg)
12 {
13 var data=msg.bbslist;
14 //$("#databox").html(msg);
15 //cleartext();
16 $.each(data,function(i,n)
17 {
18 var row=$("#temp").clone();
19 row.find("#listtile").text(n.listtile);
20 row.find("#listvalue").text(n.listvalue);
21 row.attr("id","ready");
22 row.appendTo("#mainbox");
23 }
24 );
25
26 }
27 }
28 );
29 }
html代码
2 {
3 $("[id=ready]").remove();//返回id=ready的所有dom元素
4
5 $.ajax(
6 {
7 type:"get",
8 dataType:"json",
9 url:"jspage.aspx",
10 data:"id=1",
11 success:function(msg)
12 {
13 var data=msg.bbslist;
14 //$("#databox").html(msg);
15 //cleartext();
16 $.each(data,function(i,n)
17 {
18 var row=$("#temp").clone();
19 row.find("#listtile").text(n.listtile);
20 row.find("#listvalue").text(n.listvalue);
21 row.attr("id","ready");
22 row.appendTo("#mainbox");
23 }
24 );
25
26 }
27 }
28 );
29 }
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" >
3<head>
4 <title>Untitled Page</title>
5 <link href="jspage.css" rel="Stylesheet" />
6 <script language="javascript" src="../jscript/jquery.js"></script>
7</head>
8<body onload="getjson();">
9<div id="setpage"></div>
10<div id="mainbox">
11 <div id="temp">
12 <div id="listtile"></div>
13 <div id="listvalue"></div>
14 </div>
15</div>
16</body>
17</html>
18
C#
2<html xmlns="http://www.w3.org/1999/xhtml" >
3<head>
4 <title>Untitled Page</title>
5 <link href="jspage.css" rel="Stylesheet" />
6 <script language="javascript" src="../jscript/jquery.js"></script>
7</head>
8<body onload="getjson();">
9<div id="setpage"></div>
10<div id="mainbox">
11 <div id="temp">
12 <div id="listtile"></div>
13 <div id="listvalue"></div>
14 </div>
15</div>
16</body>
17</html>
18
1using System;
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Text;
11/// <summary>
12/// Summary description for DataTableToJSON
13/// </summary>
14public class DataTableToJSON
15{
16 public DataTableToJSON()
17 {
18 //
19 // TODO: Add constructor logic here
20 //
21 }
22 public static string DtToSON(DataTable dt)
23 {
24 StringBuilder jsonBuilder = new StringBuilder();
25 jsonBuilder.Append("{\"");
26 jsonBuilder.Append(dt.TableName.ToString());
27 jsonBuilder.Append("\":[");
28 for (int i = 0; i < dt.Rows.Count; i++)
29 {
30 jsonBuilder.Append("{");
31 for (int j = 0; j < dt.Columns.Count; j++)
32 {
33 jsonBuilder.Append("\"");
34 jsonBuilder.Append(dt.Columns[j].ColumnName);
35 jsonBuilder.Append("\":\"");
36 jsonBuilder.Append(dt.Rows[i][j].ToString());
37 jsonBuilder.Append("\",");
38 }
39 jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
40 jsonBuilder.Append("},");
41 }
42 jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
43 jsonBuilder.Append("]");
44 jsonBuilder.Append("}");
45 return jsonBuilder.ToString();
46 }
47}
48
这就是主要的代码了,相信各位都会组合吧,我就不再多介绍了,大家体会一下哈!
2using System.Data;
3using System.Configuration;
4using System.Web;
5using System.Web.Security;
6using System.Web.UI;
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Text;
11/// <summary>
12/// Summary description for DataTableToJSON
13/// </summary>
14public class DataTableToJSON
15{
16 public DataTableToJSON()
17 {
18 //
19 // TODO: Add constructor logic here
20 //
21 }
22 public static string DtToSON(DataTable dt)
23 {
24 StringBuilder jsonBuilder = new StringBuilder();
25 jsonBuilder.Append("{\"");
26 jsonBuilder.Append(dt.TableName.ToString());
27 jsonBuilder.Append("\":[");
28 for (int i = 0; i < dt.Rows.Count; i++)
29 {
30 jsonBuilder.Append("{");
31 for (int j = 0; j < dt.Columns.Count; j++)
32 {
33 jsonBuilder.Append("\"");
34 jsonBuilder.Append(dt.Columns[j].ColumnName);
35 jsonBuilder.Append("\":\"");
36 jsonBuilder.Append(dt.Rows[i][j].ToString());
37 jsonBuilder.Append("\",");
38 }
39 jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
40 jsonBuilder.Append("},");
41 }
42 jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
43 jsonBuilder.Append("]");
44 jsonBuilder.Append("}");
45 return jsonBuilder.ToString();
46 }
47}
48