博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

c# 数据源转Json格式_应用到IPHONE数据服务后台

Posted on 2011-08-15 15:21  星尘的天空  阅读(237)  评论(0编辑  收藏  举报

连接地址: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;
}
}
}