getjson.js文件
Code1function getjson()
2 {
3 $("[id=ready]").remove();//返回id=ready的所有dom元素
4 $.ajax({
5 type:"get",
6 dataType:"json",
7 url:"json.aspx",
8 data:"id=1",
9 success:function(msg)
10 {
11 var data=msg.bbslist;
12 //$("#databox").html(msg);
13 //cleartext();
14 $.each(data,function(i,n)
15 {
16 var row=$("#temp").clone();
17 row.find("#listtile").text(n.listtile);
18 row.find("#listvalue").text(n.listvalue);
19 row.attr("id","ready");
20 row.appendTo("#mainbox");
21 });
22 }
23 });
24 }
test.html页面文件
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="jquery.js"></script>
7<script language="javascript" src="getjosn.js"></script>
8</head>
9<body onload="getjson();">
10<div id="setpage"></div>
11<div id="mainbox">
12 <div id="temp">
13 <div id="listtile"></div>
14 <div id="listvalue"></div>
15</div>
16</div>
17</body>
18</html>
19
json.aspx.cs文件
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Text;
11 /**//// <summary>
12 /// Summary description for DataTableToJSON
13 /// </summary>
14 public 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
转载于:https://www.cnblogs.com/Tim_Liu/archive/2010/11/24/1886669.html