Json.Net Demo2

新建一个空的Web项目,名称JsonServer,该网页实现Ajax数据请求和响应。

添加Newtonsoft.Json.dll的Dll引用,添加JQuery API文件,目录结构如下:

Json.Net Demo2

新建一个Person类

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
///Person 的摘要说明
/// </summary>
/// <summary>
/// 包含用户的基本信息
/// </summary>
public class Person
{
/// <summary>
/// 获取或设置用户名
/// </summary>
public string Name { get; set; } /// <summary>
/// 获取或设置用户年龄
/// </summary>
public int Age { get; set; } /// <summary>
/// 获取或设置用户性别
/// </summary>
public string Gender { get; set; } /// <summary>
/// 获取或设置用户国籍
/// </summary>
public string Country { get; set; } /// <summary>
/// 获取或设置用户电子邮箱
/// </summary>
public string Email { get; set; }
}

Person

新建一个数据操作类PersonRepository

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
///Class1 的摘要说明
/// </summary>
/// <summary>
/// 用户操作类
/// </summary>
public class PersonRepository
{
/// <summary>
/// 获取用户列表
/// </summary>
/// <returns>所有用户信息</returns>
public static List<Person> GetPersons()
{
List<Person> ps = new List<Person>();
Person p1 = new Person { Name = "Tom", Age = , Country = "US", Gender = "Male", Email = "tom@gmail.com" };
Person p2 = new Person { Name = "Jack", Age = , Country = "UK", Gender = "Male", Email = "jack@gmail.com" };
Person p3 = new Person { Name = "Eden", Age = , Country = "Canada", Gender = "Female", Email = "eden@gmail.com" };
Person p4 = new Person { Name = "Li Hua", Age = , Country = "China", Gender = "Male", Email = "lihui@163.com" };
Person p5 = new Person { Name = "Lvo", Age = , Country = "US", Gender = "Male", Email = "lvo@gmail.com" };
ps.Add(p1);
ps.Add(p2);
ps.Add(p3);
ps.Add(p4);
ps.Add(p5);
return ps;
}
}

PersonRepository

新建一个一般处理程序PersonHandler

 <%@ WebHandler Language="C#" Class="PersonHandler" %>

 using System;
using System.Web;
using System.Collections.Generic;
using Newtonsoft.Json;
/// <summary>
/// 处理用户类的请求
/// </summary>
public class PersonHandler : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
List<Person> persons = PersonRepository.GetPersons();
string json = JsonConvert.SerializeObject(persons);
context.Response.Write(json);
} public bool IsReusable
{
get
{
return false;
}
}
}

PersonHandler

添加一个Demo.html页面:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body> <div>
<table border="">
<thead>
<tr>
<td>
用户名
</td>
<td>
年龄
</td>
<td>
性别
</td>
<td>
国籍
</td>
<td>
电子邮箱
</td>
</tr>
</thead>
<tbody id="personBody">
</tbody>
</table>
</div> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$.getJSON("PersonHandler.ashx", function (data, status) {
if (status == "success") {
$.each(data, function (index, item) {
var beginTag = "<tr><td>";
var endTag = "</td></tr>";
var tag = "</td><td>";
$("#personBody").append($(beginTag + item.Name + tag + item.Age + tag + item.Gender + tag
+ item.Country + tag + item.Email + endTag));
});
}
});
});
</script> </body>
</html>

demo.htm

运行程序,在浏览器中查看Demo.html页面:

Json.Net Demo2

上一篇:ubuntu查看命令


下一篇:Python Pool