JSON

[ASP.net教程]生成json格式

字号+ 作者:H5之家 来源:H5之家 2017-02-06 11:00 我要评论( )

[ASP.net教程]生成json格式2014-08-22 18:00:36 0 html页面 input type="button" value="重新生成JSON" / javascript部分 script type="text/javascript" $(function () {//生成JSON$("#createjson").click(function () {$.post("NewsCategory.aspx?action=c

[ASP.net教程]生成json格式 2014-08-22 18:00:36

0


html页面

<input type="button" value="重新生成JSON" />

 javascript部分

<script type="text/javascript"> $(function () { //生成JSON $("#createjson").click(function () { $.post("NewsCategory.aspx?action=create", function (json) { alert(json.msg); }); }); });</script>

后台代码

  protected void Page_Load(object sender, EventArgs e) { if (Request.Headers["X-Requested-With"] != null && Request.Headers["X-Requested-With"].ToLower() == "".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);




标签:

0

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 我的世界json模型文件图文教程

    我的世界json模型文件图文教程

    2017-02-06 10:02

  • Cocos2d-x教程(22)-lua中的json解析

    Cocos2d-x教程(22)-lua中的json解析

    2017-02-06 09:00

  • 我的世界Json书指令教程

    我的世界Json书指令教程

    2017-02-06 08:01

  • 从零开始的 JSON 库教程(四):Unicode

    从零开始的 JSON 库教程(四):Unicode

    2017-02-03 18:06

网友点评
p