%@ WebHandler Language="C#" Class="UpdateSPMobile" %>
using System;
using System.Web;
using System.Collections;
using System.Data;
using System.IO;
public class UpdateSPMobile : IHttpHandler {
DbOperate dbo = null;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/xml";
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
try
{
dbo = new DbOperate();
string sql = "select * from com_yhb where IsExpired=0 and IsRSYH > 0 order by SUBSTRING(sp, 1, 1),xingming";
DataTable dt = dbo.ExecuteDataTable(sql);
string sp = dt.Rows[0]["sp"].ToString().Substring(0, 1);
int index = 0;
ArrayList list = new ArrayList();
ArrayList result = new ArrayList();
Hashtable ht = new Hashtable();
foreach (DataRow dr in dt.Rows)
{
if (index == 0)
{
ht.Add("sp", sp.ToUpper());
string[] temp = new string[2];
temp[1] = dr["xingming"].ToString();
temp[0] = dr["phone"].ToString();
list.Add(temp);
index++;
continue;
}
if (dr["sp"].ToString().Substring(0, 1) == sp)
{
string[] temp = new string[2];
temp[1] = dr["xingming"].ToString();
temp[0] = dr["phone"].ToString();
list.Add(temp);
}
else
{
ht.Add("children", list);
result.Add(ht);
sp = dr["sp"].ToString().Substring(0, 1);
ht = new Hashtable();
ht.Add("sp", sp.ToUpper());
list = new ArrayList();
}
}
ht.Add("children", list);
result.Add(ht);
string nt = Newtonsoft.Json.JsonConvert.SerializeObject(result);
//生成Json文件
string wwwroot = context.Request.ApplicationPath;
if (wwwroot == "/") wwwroot = "";
string filePath = context.Server.MapPath(wwwroot + "/wx/app_wx/json/allPeople_py.json");
if (File.Exists(filePath))
{
File.Delete(filePath);
}
StreamWriter sw = new StreamWriter(filePath);
sw.Write(nt);
sw.Flush();
sw.Close();
context.Response.Write("ok");
}
catch (Exception e)
{
context.Response.Write("" + e.Message + "");
}
}
public bool IsReusable {
get {
return false;
}
}
}