日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
闡述返回JSON數(shù)據(jù)的使用說明介紹

因項目需要,在苦痛掙扎了一天后,做出了一個返回JSON數(shù)據(jù)。先放在網(wǎng)上,***大家看下有什么地方需要優(yōu)化沒有,第二是避免其他ASP.NET程序員為網(wǎng)上苦苦搜索而找不到相關(guān)代碼而郁悶。

專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、做網(wǎng)站服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)鎮(zhèn)沅免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了數(shù)千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。

***步:為了使服務(wù)器端查詢返回JSON數(shù)據(jù),從網(wǎng)上找了個用C#寫的JSONHelper類(在此感謝那位不知名的好人),以下是JsonHelper類全部代碼。(JsonHelper.cs)

 
 
  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5. using System.Web.Script.Serialization;  
  6. ///  
  7. /// JSONHelper 的摘要說明  
  8. ///  
  9. public class JSONHelper  
  10. {  
  11.     //對應(yīng)JSON的singleInfo成員  
  12.     public string singleInfo = string.Empty;  
  13.     protected string _error = string.Empty;  
  14.     protected bool _success = true;  
  15.     protected long _totalCount = 0;  
  16.     protected System.Collections.ArrayList arrData = new ArrayList();  
  17.     protected System.Collections.ArrayList arrDataItem = new ArrayList();  
  18.     public JSONHelper()  
  19.     {  
  20.     }  
  21.     public static string ToJSON(object obj)  
  22.     {  
  23.         JavaScriptSerializer serializer = new JavaScriptSerializer();  
  24.         return serializer.Serialize(obj);  
  25.     }  
  26.     public static string ToJSON(object obj, int recursionDepth)  
  27.     {  
  28.         JavaScriptSerializer serializer = new JavaScriptSerializer();  
  29.         serializer.RecursionLimit = recursionDepth;  
  30.         return serializer.Serialize(obj);  
  31.     }  
  32.     //對應(yīng)于JSON的success成員  
  33.     public bool success  
  34.     {  
  35.         get  
  36.         {  
  37.             return _success;  
  38.         }  
  39.         set  
  40.         {  
  41.             //如設(shè)置為true則清空error  
  42.             if (success) _error = string.Empty;  
  43.             _success = value;  
  44.         }  
  45.     }  
  46.     //對應(yīng)于JSON的error成員  
  47.     public string error  
  48.     {  
  49.         get  
  50.         {  
  51.             return _error;  
  52.         }  
  53.         set  
  54.         {  
  55.             //如設(shè)置error,則自動設(shè)置success為false  
  56.             if (value != "") _success = false;  
  57.             _error = value;  
  58.         }  
  59.     }  
  60.     public long totlalCount  
  61.     {  
  62.         get { return _totalCount; }  
  63.         set { _totalCount = value; }  
  64.     }  
  65.     //重置,每次新生成一個json對象時必須執(zhí)行該方法  
  66.     public void Reset()  
  67.     {  
  68.         _success = true;  
  69.         _error = string.Empty;  
  70.         singleInfo = string.Empty;  
  71.         arrData.Clear();  
  72.         arrDataItem.Clear();  
  73.     }  
  74.     public void AddItem(string name, string value)  
  75.     {  
  76.         arrData.Add("\"" + name + "\":" + "\"" + value + "\"");  
  77.     }  
  78.     public void ItemOk()  
  79.     {  
  80.         arrData.Add("
    ");  
  81.     }  
  82.     //序列化JSON對象,得到返回的JSON代碼  
  83.     public override string ToString()  
  84.     {  
  85.         StringBuilder sb = new StringBuilder();  
  86.         sb.Append("{");  
  87.         sb.Append("totalCount:" + totlalCount.ToString() + ",");  
  88.         sb.Append("success:" + _success.ToString().ToLower() + ",");  
  89.         sb.Append("error:\"" + _error.Replace("\"", "\\\"") + "\",");  
  90.         sb.Append("singleInfo:\"" + singleInfo.Replace("\"", "\\\"") + "\",");  
  91.         sb.Append("data:[");  
  92.         int index = 0;  
  93.         sb.Append("{");  
  94.         if (arrData.Count <= 0)  
  95.         {  
  96.             sb.Append("}]");  
  97.         }  
  98.         else  
  99.         {  
  100.             foreach (string val in arrData)  
  101.             {  
  102.                 index++;  
  103.                 if (val != "
    ")  
  104.                 {  
  105.                     sb.Append(val + ",");  
  106.                 }  
  107.                 else  
  108.                 {  
  109.                     sbsb = sb.Replace(",", "", sb.Length - 1, 1);  
  110.                     sb.Append("},");  
  111.                     if (index < arrData.Count)  
  112.                     {  
  113.                         sb.Append("{");  
  114.                     }  
  115.                 }  
  116.             }  
  117.             sbsb = sb.Replace(",", "", sb.Length - 1, 1);  
  118.             sb.Append("]");  
  119.         }  
  120.         sb.Append("}");  
  121.         return sb.ToString();  
  122.     }  

第二步:新建ASPX文件,做為服務(wù)器端,用返回查詢的JSON數(shù)據(jù)(PagingRequest.cs)

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using System.Text;  
  10. namespace ExtJSDemo.Data  
  11. {  
  12.     public partial class PagingRequest : System.Web.UI.Page  
  13.     {  
  14.         protected void Page_Load(object sender, EventArgs e)  
  15.         {  
  16.             int start = Convert.ToInt32(Request["start"].ToString());   // ExtJS Paging 必須指定的參數(shù)(從第幾行記錄開始)/ ExtJS默認(rèn)0是***行記錄  
  17.             int limit = Convert.ToInt32(Request["limit"].ToString()); // ExtJS Paging 必須指定的參數(shù)(每頁顯示多少行記錄)  
  18.             JSONHelper jsonHelp = new JSONHelper();  
  19.             int TotalRecords = 0;   
  20.             DataSet DSet = GET_Product_Data(start, limit, out TotalRecords); // 獲取數(shù)據(jù)  
  21.             jsonHelp.success = true;  
  22.             jsonHelp.totlalCount = TotalRecords;// 記錄總數(shù)  
  23.             if (DSet != null)  
  24.             {  
  25.                 DataTable DTable = DSet.Tables[0];  
  26.                 for (int i = 0; i < DTable.Rows.Count; i++)  
  27.                 {  
  28.                     // 循環(huán)生成JSON代碼  
  29.                     jsonHelp.AddItem("Id", DTable.Rows[i]["Id"].ToString());  
  30.                     jsonHelp.AddItem("Name", DTable.Rows[i]["Name"].ToString());  
  31.                     jsonHelp.AddItem("StreetPrice", DTable.Rows[i]["StreetPrice"].ToString());  
  32.                     jsonHelp.AddItem("TypeName", DTable.Rows[i]["TypeName"].ToString());  
  33.                     jsonHelp.ItemOk();  
  34.                 }  
  35.             }  
  36.             Response.Write(jsonHelp.ToString());  // 輸出JSON代碼  
  37.         }  
  38.         ///  
  39.         /// 獲取數(shù)據(jù)  
  40.         ///  
  41.         ///  name="PageIndex">記錄索引 
  42.         ///  name="PageSize">每頁顯示記錄數(shù) 
  43.         ///  name="TotalRecords">總記錄數(shù) 
  44.         public DataSet GET_Product_Data(int start, int limit,out int TotalRecords)  
  45.         {  
  46.             SqlConnection _Connection = null;  
  47.             SqlDataAdapter _Adapter = null;  
  48.             DataSet DSet = null;  
  49.             try  
  50.             {  
  51.                 _Connection = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["SQLContionString"].ToString());  
  52.                 _Connection.Open();  
  53.                 _Adapter = new SqlDataAdapter("SELECT [Id] FROM [Product] ",_Connection);  
  54.                 DSet = new DataSet();  
  55.                 _Adapter.Fill(DSet);  
  56.                 TotalRecords = Convert.ToInt32(DSet.Tables[0].Rows.Count);      // 記錄總數(shù)  
  57.                 if (TotalRecords < 1) return null;  // 沒有記錄  
  58.                 int pageLowerBound = start+1;                                            // 從第幾條數(shù)據(jù)開始  
  59.                 int pageUpperBound = pageLowerBound + limit;             // 每頁多少條數(shù)據(jù)  
  60.                 StringBuilder sb = new StringBuilder();  
  61.                 if (TotalRecords >= pageLowerBound)  
  62.                 {  
  63.                     for (int i = pageLowerBound; i < TotalRecords && i < pageUpperBound; i++)  
  64.                     {  
  65.                         sb.AppendFormat("'{0}',", DSet.Tables[0].Rows[i-1][0].ToString());//構(gòu)造ID in() 條件,取其中一頁  
  66.                     }  
  67.                 }  
  68.                 else   
  69.                     return null; // 沒有記錄  
  70.                 if (sb.Length > 1)  
  71.                     sb.Remove(sb.Length - 1, 1);//刪除***一個逗號  
  72.                 StringBuilder strSql = new StringBuilder();  
  73.                 strSql.Append("SELECT a.[Id],a.[Name],a.[StreetPrice],b.TypeName  ");  
  74.                 strSql.Append(" FROM [Product] as a left join ProductType  as b on a.Typeid = b.Typeid ");  
  75.                 strSql.AppendFormat(" where a.[Id] in({0})", sb.ToString());  
  76.                 _Adapter = new SqlDataAdapter(strSql.ToString(), _Connection);  
  77.                 DSet = new DataSet();  
  78.                 _Adapter.Fill(DSet);  
  79.             }  
  80.             finally  
  81.             {  
  82.                 if (_Connection != null && _Connection.State == ConnectionState.Open)  
  83.                 {  
  84.                     _Connection.Close();  
  85.                 }  
  86.             }  
  87.             return DSet;  
  88.         }  
  89.     }  
  90. }  

 第三步:返回JSON數(shù)據(jù)頁面用作客戶端呈現(xiàn)結(jié)果的載體(PagingControls.aspx)

 
 
  1.  id="Head1" runat="server"> 
  2.      
  3.          rel="stylesheet" type="text/css" href="../ext3/resources/css/ext-all.css" mce_href="ext3/resources/css/ext-all.css" /> 
  4.      type="text/javascript" src="../ext3/adapter/ext/ext-base.js" mce_src="ext3/adapter/ext/ext-base.js"> 
  5.      type="text/javascript" src="../ext3/ext-all.js" mce_src="ext3/ext-all.js"> 
  6.      type="text/javascript" src="JS/PagingGridPanel.js" mce_src="JS/PagingGridPanel.js"> 
  7.          type="text/javascript">
  8. <dfn id="hhbdb"><dl id="hhbdb"></dl></dfn>
    <small id="hhbdb"></small>
    <tt id="hhbdb"></tt>
    <td id="hhbdb"><rp id="hhbdb"><thead id="hhbdb"></thead></rp></td>
    • <tt id="hhbdb"></tt>
      <small id="hhbdb"><tt id="hhbdb"></tt></small>
      • <menuitem id="hhbdb"><dl id="hhbdb"><em id="hhbdb"></em></dl></menuitem>