http://hi.baidu.com/cxzhang/blog/item/0166563892cc65fbb211c7b0.html.
http://topic.youkuaiyun.com/t/20030527/22/1842509.html
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Reflection;
public class ToJson
{
/// <summary>
/// datatable to json
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string DataTableToJson(DataTable dt)
{
StringBuilder JsonString = new StringBuilder();
if (dt != null && dt.Rows.Count > 0)
{
JsonString.Append("[ ");
for (int i = 0; i < dt.Rows.Count; i++)
{
JsonString.Append("{ ");
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j ==0)
{
JsonString.Append("/"" + dt.Columns[j].ColumnName.ToString() + "/":" + "/"" + dt.Rows[i][j].ToString() + "/"");
}
else
{
JsonString.Append(",/"" + dt.Columns[j].ColumnName.ToString() + "/":" + "/"" + dt.Rows[i][j].ToString() + "/"");
}
}
/**/
/*end Of String*/
if (i == dt.Rows.Count - 1)
{
JsonString.Append("} ");
}
else
{
JsonString.Append("}, ");
}
}
JsonString.Append("]");
return JsonString.ToString();
}
else
{
return null;
}
}
}
前台
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">
$(function(){
//点击按钮,并执行ajax请求
$("#ajaxReuqestID").click(function(){
// $.ajax({
// url: "Handler.ashx",
// beforeSend:function(){
// $("#ajaxStateID").text("berforeSend");
// alert("berforeSend");
// },
// success: function(html){
// $("#ajaxStateID").text("success");
// // $("#aaa").append(html);
// alert(html);
// },
// error:function(){
// $("#ajaxStateID").text("error");
// alert("error");
// },
// complete:function(){
// $("#ajaxStateID").text("complete");
// alert("complete");
// }
// });
//$.getScript("t.js", function(){
// alert("Script loaded and executed.");
//});
$.getJSON(
"Handler.ashx",
function(json){
$.each(json,function(i){
// var s="asdfasdf";
// alert(s);
// json=[{"name":"fan","age":26},{"name":"wang","age":25}];
$("#cat-list").append("<li>name:"+json[i].name+" Age:"+json[i].age+" Age:"+json[i].pwd+"</li>")
})
});
// var jsonStr = "{/"a/":/"内容1/", /"b/":/"内容2/"}";
// $.each(jsonStr,function(i){
// $("#aaa").append("<li>name:"+jsonStr[i].a+" Age:"+jsonStr[i].b+"</li>")
// })
// });
// var jsonObj = eval("(" + jsonStr + ")");
// for(var property in jsonObj){
// var nodeObj = document.getElementById(property);
// if(nodeObj)
// nodeObj.childNodes[0].nodeValue = jsonObj[property];
// }
});
// $("#ajaxStateID").ajaxStart(function(){
// $(this).text("ajaxStart");
// alert("ajaxStart");
// }).ajaxSend(function(){
// $(this).text("ajaxSend");
// alert("ajaxSend");
// }).ajaxSuccess(function(){
// $(this).text("ajaxSuccess");
// alert("ajaxSuccess");
// }).ajaxError(function(){
// $(this).text("ajaxError");
// alert("ajaxError");
// }).ajaxComplete(function(){
// $(this).text("ajaxComplete");
// alert("ajaxComplete");
// }).ajaxStop(function(){
// $(this).text("ajaxStop");
// alert("ajaxStop");
// });
})
</script>
</head>
<body>
<input type="button" value="点击触发ajax请求" id="ajaxReuqestID"/>
<div id="ajaxStateID"></div>
<div id="aaa">
</div>
<ul id="cat-list"></ul>
</body>
</html>