html页面
<input type="button" value="重新生成JSON" class="button1" id="createjson" />
<script type="text/javascript"> $(function () { //生成JSON $("#createjson").click(function () { $.post("NewsCategory.aspx?action=create", function (json) { alert(json.msg); }); }); }); </script>
后台代码
logs_code_hide('015d4d42-5eb8-4225-a777-86e3a1eb0da0',event)" src="/Upload/Images/2014082218/2B1B950FA3DF188F.gif" alt="" />protected void Page_Load(object sender, EventArgs e) { if (Request.Headers["X-Requested-With"] != null && Request.Headers["X-Requested-With"].ToLower() == "XMLHttpRequest".ToLower()) { Response.Clear(); Response.ContentType = "application/json"; if (Request["action"] == "create") { Response.Write(CreateJson()); } Response.End(); } }View Code
/// <summary> /// 生成商家类别Json /// </summary> /// <returns></returns> protected string CreateJson() { System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer(); //初始化(引用空间using System.Text;)
StringBuilder sb = new StringBuilder(); var list = Express.BLL.NewsCategory.Get(); foreach (var item in list) { if (sb.Length > 0) sb.Append(","); sb.Append(string.Format("{{\"id\":{0},\"name\":\"{1}\",\"pid\":{2},\"itemvalue\":\"{3}\"", item.Id, GetUnicode(item.ItemName), item.ParentId, item.ItemValue)); sb.Append("}"); } //返回与Web服务器上的指定虚拟路径相对应的物理文件路径
string filePath = Server.MapPath(@"/common/newscategoryJson.js"); //创建一个新文件,并写入指定字符串,若目标文件已存在,则覆盖该文件
System.IO.File.WriteAllText(filePath, "[" + sb.ToString() + "]", System.Text.Encoding.UTF8); return json.Serialize(new { code = 1, msg = "生成完成" }); }
汉字转为Unicode编码
/// <summary> /// 得到汉字的Unicode编码 /// </summary> protected string GetUnicode(string text) { string result = ""; for (int i = 0; i < text.Length; i++) { if ((int)text[i] > 32 && (int)text[i] < 127) { result += text[i].ToString(); } else result += string.Format("\\u{0:x4}", (int)text[i]); } return result; }View Code
数据库部分
/// 获取全部 /// </summary> public List<Model.NewsCategory> Get() { string sql = "select * from NewsCategory order by case when ParentId=0 then Id*10000 else ParentId*10000+Id end"; List<Model.NewsCategory> list = new List<Model.NewsCategory>(); using (SqlDataReader dr = DBUtility.SqlHelper.ExecuteReader(ConnString.connReadonly, CommandType.Text, sql, null)) { while (dr.Read()) { Model.NewsCategory model = new Model.NewsCategory(); object obj; obj = dr["Id"]; if (obj != null && obj != DBNull.Value) { model.Id = (int)obj; } obj = dr["SortValue"]; if (obj != null && obj != DBNull.Value) { model.SortValue = (int)obj; } obj = dr["ParentId"]; if (obj != null && obj != DBNull.Value) { model.ParentId = (int)obj; } model.ItemName = dr["ItemName"].ToString(); model.ItemValue=dr["ItemValue"].ToString(); list.Add(model); } } return list; }View Code
运行结果
common/newscategoryJson.js
[
{"id":31,"name":"\u65b0\u95fb\u4e2d\u5fc3","pid":0,"itemvalue":"|0|"},
{"id":51,"name":"\u4f01\u4e1a\u5feb\u8baf","pid":31,"itemvalue":"|0|31|"},
{"id":52,"name":"\u4f01\u4e1a\u516c\u544a","pid":31,"itemvalue":"|0|31|"},
{"id":53,"name":"\u884c\u4e1a\u52a8\u6001","pid":31,"itemvalue":"|0|31|"},
{"id":91,"name":"\u65b0\u95fb\u4e2d\u5fc3","pid":31,"itemvalue":"|0|31|"},
{"id":93,"name":"\u4f01\u4e1a\u5feb\u8baf","pid":31,"itemvalue":"|0|31|"},
{"id":94,"name":"\u4f01\u4e1a\u516c\u544a","pid":31,"itemvalue":"|0|31|"},
{"id":180,"name":"\u6d4b\u8bd5","pid":31,"itemvalue":"|0|31|"},
{"id":181,"name":"\u6d4b\u8bd5111","pid":31,"itemvalue":"|0|31|"},
{"id":54,"name":"\u5173\u4e8e\u76df\u53cb\u634c\u634c","pid":0,"itemvalue":"|0|"},
{"id":55,"name":"\u4f01\u4e1a\u6982\u51b5","pid":54,"itemvalue":"|0|54|"},
{"id":56,"name":"\u4f01\u4e1a\u6587\u5316","pid":54,"itemvalue":"|0|54|"},
{"id":57,"name":"\u4f01\u4e1a\u53d1\u5c55","pid":54,"itemvalue":"|0|54|"}
......
]
涉及的知识点
1、Server.MapPath(string path);
2、System.IO.File.WriteAllText(sting path,string contents,Encoding encoding);