转自:http://www.cnblogs.com/symjie520/archive/2008/04/16/1155997.html

 今天使用了jquery提供的对json的操作的函数,感觉超级爽哦!下面就把代码贴出
js

 1 function 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代码

 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#

 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 - 11);
40            jsonBuilder.Append("},");
41        }

42        jsonBuilder.Remove(jsonBuilder.Length - 11);
43        jsonBuilder.Append("]");
44        jsonBuilder.Append("}");
45        return jsonBuilder.ToString();
46    }

47}

48

这就是主要的代码了,相信各位都会组合吧,我就不再多介绍了,大家体会一下哈!

posted on 2009-05-22 18:21  jackljf  阅读(150)  评论(0编辑  收藏  举报