国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

生成json格式

2019-11-17 01:42:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

生成json格式

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() == "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服務(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("http://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);


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 松潘县| 霍州市| 彰武县| 桃园县| 佳木斯市| 湟源县| 庆安县| 云浮市| 太原市| 德令哈市| 孟州市| 松滋市| 山东省| 渑池县| 招远市| 修水县| 嘉定区| 吉木萨尔县| 阆中市| 惠州市| 绥棱县| 清镇市| 新巴尔虎右旗| 辽源市| 南丹县| 布尔津县| 四会市| 昌宁县| 深水埗区| 沈丘县| 陆丰市| 寿宁县| 金湖县| 徐州市| 湾仔区| 永安市| 东乡族自治县| 莫力| 嵊州市| 德惠市| 玉环县|