一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

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

 偷心無痕 2014-11-13

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

0

html頁(yè)面

 <input type="button" value="重新生成JSON" class="button1" id="createjson" />

 javascript部分

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

后臺(tái)代碼

  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服務(wù)器上的指定虛擬路徑相對(duì)應(yīng)的物理文件路徑
string filePath = Server.MapPath(@"/common/newscategoryJson.js");     //創(chuàng)建一個(gè)新文件,并寫入指定字符串,若目標(biāo)文件已存在,則覆蓋該文件
System.IO.File.WriteAllText(filePath, "[" + sb.ToString() + "]", System.Text.Encoding.UTF8); return json.Serialize(new { code = 1, msg = "生成完成" }); }

 漢字轉(zhuǎn)為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

數(shù)據(jù)庫(kù)部分

    /// 獲取全部    /// </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

運(yùn)行結(jié)果
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|"}
......
]

 涉及的知識(shí)點(diǎn)

1、Server.MapPath(string path);

2、System.IO.File.WriteAllText(sting path,string contents,Encoding encoding);




標(biāo)簽:
0

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    日本久久中文字幕免费| 久久99国产精品果冻传媒| 中日韩免费一区二区三区| 欧美一区二区三区在线播放| 日本午夜免费啪视频在线| 99久久精品久久免费| 色小姐干香蕉在线综合网| 亚洲欧美精品伊人久久| 国内精品伊人久久久av高清| 五月天婷亚洲天婷综合网| 中文字幕91在线观看| 一区二区三区日韩经典| 日本乱论一区二区三区| 日韩欧美在线看一卡一卡| 亚洲欧美精品伊人久久| 韩国日本欧美国产三级| 中文字幕乱码一区二区三区四区| 东京热男人的天堂一二三区| 国产又色又粗又黄又爽| 欧美不雅视频午夜福利| 国产成人在线一区二区三区| 欧美一区二区三区不卡高清视| 国产午夜福利不卡片在线观看| 日本高清中文精品在线不卡| 妻子的新妈妈中文字幕| 黄色国产自拍在线观看| 欧美日韩国产免费看黄片| 91偷拍裸体一区二区三区| 女厕偷窥一区二区三区在线| 久久福利视频在线观看| 国产成人亚洲综合色就色| 午夜免费精品视频在线看| 加勒比系列一区二区在线观看 | 日本女优一色一伦一区二区三区| 中国黄色色片色哟哟哟哟哟哟| 我想看亚洲一级黄色录像| 久久国产精品亚州精品毛片| 九九热最新视频免费观看| 午夜福利视频偷拍91| 日韩免费av一区二区三区| 香蕉尹人视频在线精品|