DataTable 和Json 字串互轉

來源:互聯網
上載者:User

標籤:

#region  DataTable 轉換為Json字串執行個體方法/// <summary>/// GetClassTypeJosn 的摘要說明/// </summary>public class GetClassTypeJosn : IHttpHandler{    /// <summary>    /// 檔案名稱:DataTable 和Json 字串互轉/// </summary>     //用法說明執行個體     public void ProcessRequest(HttpContext context)    {        context.Response.ContentType = "application/json";        context.Response.Charset = "utf-8";        HttpRequest req = context.Request;        string method = req["method"].ToStr().ToLower();       //擷取合約明細列表  DataTable 轉換為Json字串        if (method == "txtdate")        {            string json = "";            BO.MakeContractMx bll = new MakeContractMx();            DataSet ds = bll.GetDataTable();            if (ds.Tables.Count > 0)            {                json =ToJson(ds.Tables[0]);            }            context.Response.Write(json);            return;        }    }   public bool IsReusable    {        get        {            return false;        }    }}   #endregion    #region Json字串轉換為DataTable 執行個體方法    public DataTable JsonToDataTable(json)    {       DataTable  dt= ToDataTable(json);         return dt;    }        #endregion    #region DataTable 轉換為Json 字串    /// <summary>    /// DataTable 對象 轉換為Json 字串    /// </summary>    /// <param name="dt"></param>    /// <returns></returns>    public static string ToJson(this DataTable dt)    {        JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();        javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大數值        ArrayList arrayList = new ArrayList();        foreach (DataRow dataRow in dt.Rows)        {            Dictionary<string, object> dictionary = new Dictionary<string, object>();  //執行個體化一個參數集合            foreach (DataColumn dataColumn in dt.Columns)            {                dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToStr());            }            arrayList.Add(dictionary); //ArrayList集合中添加索引值        }        return javaScriptSerializer.Serialize(arrayList);  //返回一個json字串    }    #endregion    #region Json 字串 轉換為 DataTable資料集合    /// <summary>    /// Json 字串 轉換為 DataTable資料集合    /// </summary>    /// <param name="json"></param>    /// <returns></returns>    public static DataTable ToDataTable(this string json)    {        DataTable dataTable = new DataTable();  //執行個體化        DataTable result;        try        {            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();            javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大數值            ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);            if (arrayList.Count > 0)            {                foreach (Dictionary<string, object> dictionary in arrayList)                {                    if (dictionary.Keys.Count<string>() == 0)                    {                        result = dataTable;                        return result;                    }                    if (dataTable.Columns.Count == 0)                    {                        foreach (string current in dictionary.Keys)                        {                            dataTable.Columns.Add(current, dictionary[current].GetType());                        }                    }                    DataRow dataRow = dataTable.NewRow();                    foreach (string current in dictionary.Keys)                    {                        dataRow[current] = dictionary[current];                    }                    dataTable.Rows.Add(dataRow); //迴圈添加行到DataTable中                }            }        }        catch        {        }        result = dataTable;        return result;    }    #endregion    #region 轉換為string字串類型    /// <summary>    ///  轉換為string字串類型    /// </summary>    /// <param name="s">擷取需要轉換的值</param>    /// <param name="format">需要格式化的位元</param>    /// <returns>返回一個新的字串</returns>    public static string ToStr(this object s, string format = "")    {        string result = "";        try        {            if (format == "")            {                result = s.ToString();            }            else            {                result = string.Format("{0:" + format + "}", s);            }        }        catch        {        }        return result;    }   #endregion

 

DataTable 和Json 字串互轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.