list集合转换为json数据
<%@ WebHandler Language="C#" Class="MailReader" %>using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
public class MailReader : IHttpHandler {
List<int> list = null;
string jsonName = "Source";
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//OATableAdapters.MailTableAdapter ada = new OATableAdapters.MailTableAdapter();
//ada.GetData().WriteXml(@"c:\mail.xml");
//OATableAdapters.UserListTableAdapter ada = new OATableAdapters.UserListTableAdapter();
//ada.GetData().WriteXml(@"c:\user.xml");
//代办总条数
OATableAdapters.DataTable1TableAdapter db = new OATableAdapters.DataTable1TableAdapter();
OATableAdapters.DataTable1TableAdapter sp = new OATableAdapters.DataTable1TableAdapter();
OATableAdapters.DataTable1TableAdapter zh = new OATableAdapters.DataTable1TableAdapter();
int daiban = (int)db.ScalarQuery();
int shenpi = (int)sp.ShenPi();
int zhonghe = (int)zh.ZongHe();
list = new List<int>();
list.Add(daiban);
list.Add(shenpi);
list.Add(zhonghe);
string sdas = ObjectToJson<int>(jsonName, list);
}
public string ObjectToJson<T>(string jsonName, List<T> IL)
{
StringBuilder Json = new System.Text.StringBuilder();
Json.Append("{\"" + jsonName + "\":[");
if (IL.Count > 0)
{
for (int i = 0; i < IL.Count; i++)
{
Object obj = Activator.CreateInstance<int>();
PropertyInfo[] pis = obj.GetType().GetProperties();
Json.Append("{");
for (int j = 0; j < pis.Length; j++)
{
Json.Append("\"" + pis[j].Name.ToString() + "\":\"" + pis[j].GetValue(IL[i], null) + "\"");
if (j < pis.Length - 1)
{
Json.Append(",");
}
}
Json.Append("}");
if (i < IL.Count - 1)
{
Json.Append(",");
}
}
}
Json.Append("]}");
return Json.ToString();
}
public bool IsReusable {
get {
return false;
}
}
}