context.Response.ContentType = \"text/plain\"; DataTable data2 = myData();
string str = DataTableToJson(\"myJson\构建的json数据
context.Response.Write(str); }
public bool IsReusable {
get {
return false; } }
//用datatable做数据,数据模拟 public DataTable myData() {
DataTable dt = new DataTable(); DataColumn dc; DataRow dr;
dc = new DataColumn(\"Name\ dt.Columns.Add(dc);
dc = new DataColumn(\"Email\ dt.Columns.Add(dc);
dc = new DataColumn(\"Phon\ dt.Columns.Add(dc);
dc = new DataColumn(\"Move\ dt.Columns.Add(dc);
dc = new DataColumn(\"Pan\ dt.Columns.Add(dc);
dc = new DataColumn(\"select\ dt.Columns.Add(dc); for (int i = 1; i < 7; i++) {
dr = dt.NewRow();
dr[\"Name\"] = \"Name\" + i.ToString(); dr[\"Email\"] = \"Email\" + i.ToString(); dr[\"Phon\"] = \"Phon\" + i.ToString();
dr[\"Move\"] = \"Move\" + i.ToString(); dr[\"Pan\"] = \"Pan\" + i.ToString(); dr[\"select\"] = i.ToString(); dt.Rows.Add(dr); }
int count = dt.Rows.Count; return dt; }
//将datatable数据转换成JSON数据
public string DataTableToJson(string jsonName, DataTable dt) {
StringBuilder Json = new StringBuilder(); Json.Append(\"[\"); if (dt.Rows.Count > 0) {
for (int i = 0; i < dt.Rows.Count; i++) {
Json.Append(\"{\");
for (int j = 0; j < dt.Columns.Count; j++) {
Json.Append(dt.Columns[j].ColumnName.ToString() dt.Rows[i][j].ToString() + \"\\\"\");
if (j < dt.Columns.Count - 1) {
Json.Append(\ } }
Json.Append(\
if (i < dt.Rows.Count - 1) {
Json.Append(\ } } }
Json.Append(\"]\"); return Json.ToString(); }
//列表数据转换到json数据
public string ObjectToJson StringBuilder Json = new StringBuilder(); + \":\\\"\" + Json.Append(\"[\"); if (IL.Count > 0) { for (int i = 0; i < IL.Count; i++) { T obj = Activator.CreateInstance PropertyInfo[] pis = type.GetProperties(); Json.Append(\"{\"); for (int j = 0; j < pis.Length; j++) { Json.Append(pis[j].Name.ToString() + \":\\\"\" + pis[j].GetValue(IL[i], null) + \"\\\"\"); if (j < pis.Length - 1) { Json.Append(\ } } Json.Append(\ if (i < IL.Count - 1) { Json.Append(\ } } } Json.Append(\"]\"); return Json.ToString(); } 页面Default.aspx function GetData() { $.getJSON( \"JsonData.ashx\产生Json数据的服务端页面 function(json) { for(var i=0;i var index=-1; var txtTRLastIndex = findObj(\"txtTRLastIndex\ var rowID = parseInt(txtTRLastIndex.value); var signFrame = findObj(\"SignFrame\ //添加行 var newTR = signFrame.insertRow(signFrame.rows.length); newTR.id = \"SignItem\" + rowID; //添加列:序号 var newNameTD=newTR.insertCell(0); //添加列内容 newNameTD.innerHTML = newTR.rowIndex.toString(); //添加列:姓名 } var newNameTD=newTR.insertCell(1); 因篇幅问题不能全部显示,请点此查看更多更全内容