%@ Page Language="C#" %>
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string jsonStr = string.Empty;
ListTestData> tds = new ListTestData>();
//测试数据
for (int i = 1; i 4; i++)
{
tds.Add(new TestData() { Id = i, Name = "jinho" + i, Sex = "male" });
} //把一个list转换为json字符串
jsonStr = JsonHelper.GetJsonListTestData>>(tds);
Response.Write(jsonStr);
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "json", "getJson(" + jsonStr + ");", true);
}
/script>
script type="text/javascript">
function getJson(jsonStr) { //使用eval函数
var json = eval(jsonStr); //因为上面为list集合
for (var i = 0; i json.length; i++) {
alert(json[i].Id + "Name:" + json[i].Name);
}
}
/script>
html xmlns="http://www.w3.org/1999/xhtml">
head runat="server">
title>/title>
/head>
body>
form id="form1" runat="server">
div>
/div>
/form>
/body>
/html>