连接地址:http://www.cnblogs.com/xinjian/archive/2010/11/23/1885225.html
新建一个一般处理程序,命名为JsonTransHander.ashx。内容如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
namespace auotoCompleteText
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class JsonTransHander : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//初始化数据
List<People> peopleList = new List<People>();
for (int i = 0; i < 10; i++)
{
People people = new People() { ID=i,Name="name"+i};
peopleList.Add(people);
}
//引用System.Web.Extensions .net3.5框架
JavaScriptSerializer serializer=new JavaScriptSerializer();
//转换
var jsonData= serializer.Serialize(peopleList);
//返回
context.Response.Write(jsonData);
}
public bool IsReusable
{
get
{
return false;
}
}
}
public class People
{
public string Name
{
set;
get;
}
public int ID
{
set;
get;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
namespace auotoCompleteText
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class JsonTransHander : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//初始化数据
List<People> peopleList = new List<People>();
for (int i = 0; i < 10; i++)
{
People people = new People() { ID=i,Name="name"+i};
peopleList.Add(people);
}
//引用System.Web.Extensions .net3.5框架
JavaScriptSerializer serializer=new JavaScriptSerializer();
//转换
var jsonData= serializer.Serialize(peopleList);
//返回
context.Response.Write(jsonData);
}
public bool IsReusable
{
get
{
return false;
}
}
}
public class People
{
public string Name
{
set;
get;
}
public int ID
{
set;
get;
}
}
}