net向其他程序提供Json接口数据
/// <summary> /// Get_Nav 的摘要说明 /// </summary> public class Get_Nav : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; BLL.HD_News_Category Bclass = new BLL.HD_News_Category(); BLL.HD_News BNews = new BLL.HD_News(); BLL.HD_dtJB BdtJB = new BLL.HD_dtJB(); DataTable dtYC = new DataTable(); DataTable dtZL = new DataTable(); string navstr = "{\"navlist\":["; navstr = navstr + "{"; navstr = navstr + "\"name\":\"XX议程\","; navstr = navstr + "\"sub_nav\":["; //二级菜单 dtYC = Bclass.GetList(" parent_id=38").Tables[0]; navstr = navstr + Maticsoft.Common.ConvertJson.ToJson(dtYC); navstr = navstr + "]"; navstr = navstr + "},"; navstr = navstr + "{"; navstr = navstr + "\"name\":\"历年数据资料\","; navstr = navstr + "\"sub_nav\":["; //二级菜单 dtZL = BNews.GetList(0, "yearage", " category_id=39 and status=0 group by yearage ", " yearage desc").Tables[0]; navstr = navstr + Maticsoft.Common.ConvertJson.ToJson(dtZL); navstr = navstr + "]"; navstr = navstr + "}"; navstr = navstr + "]"; navstr = navstr + "}"; context.Response.Write(navstr); } public bool IsReusable { get { return false; } } }
//单篇文章获取数据
context.Response.ContentType = "text/plain";
DataTable newslcasst = new DataTable();
BLL.News Bnews = new BLL.News();
string id = "0";
if (context.Request["id"] != null && context.Request["id"] != "")
{
id = context.Request["id"];
newslcasst = Bnews.GetList("id=" + Convert.ToInt32(id) + "").Tables[0];
}
context.Response.Write(Maticsoft.Common.ConvertJson.ToJson(newslcasst));
/// <summary> /// Datatable转换为Json /// </summary> /// <param name="table">Datatable对象</param> /// <returns>Json字符串</returns> public static string ToJson(DataTable dt) { StringBuilder jsonString = new StringBuilder(); if (dt.Rows.Count > 0) { jsonString.Append("["); DataRowCollection drc = dt.Rows; for (int i = 0; i < drc.Count; i++) { jsonString.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { string strKey = dt.Columns[j].ColumnName; string strValue = drc[i][j].ToString(); Type type = dt.Columns[j].DataType; jsonString.Append("\"" + strKey + "\":"); strValue = StringFormat(strValue, type); if (j < dt.Columns.Count - 1) { jsonString.Append(strValue + ","); } else { jsonString.Append(strValue); } } jsonString.Append("},"); } //jsonString.Append("{\"result\":\"返回数据成功!\",\"code\":\"1\"}"); jsonString.Remove(jsonString.Length - 1, 1); jsonString.Append("]"); } else { jsonString.Append("{\"result\":\"暂无数据!\",\"code\":\"0\"}"); } return jsonString.ToString(); }

浙公网安备 33010602011771号