JSONHelper JSON帮助类

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Web;  
    using System.Web.Script.Serialization;  
    using System.Data;  
    /************************************************** 
     * 版权所有:  
     * 文 件 名: JSONHelper.cs 
     * 文件描述:  
     * 类型说明: JSONHelper  JSON帮助类 
     * 授权声明: 
     *           本程序为自由软件; 
     *           您可依据自由软件基金会所发表的GPL v3授权条款,对本程序再次发布和/或修改; 
     *           本程序是基于使用目的而加以发布,然而不负任何担保责任; 
     *           亦无对适售性或特定目的适用性所为的默示性担保。 
     *           详情请参照GNU通用公共授权 v3(参见license.txt文件)。 
     * 版本历史:  
     *           v2.0.0 Sheng   2009-09-09 修改 
    ***************************************************/  
      
    /// <summary>  
    ///JSONHelper 的摘要说明  
    /// </summary>  
    public class JSONHelper  
    {  
        /// <summary>  
        /// 对象转JSON  
        /// </summary>  
        /// <param name="obj">对象</param>  
        /// <returns>JSON格式的字符串</returns>  
        public static string ObjectToJSON(object obj)  
            {  
                JavaScriptSerializer jss = new JavaScriptSerializer();  
                try  
                {  
                    return jss.Serialize(obj);  
                }  
                catch (Exception ex)  
                {  
                      
                    throw new Exception(ex.Message);  
                }  
            }  
        /// <summary>  
        /// 数据表转键值对集合  
        /// 把DataTable转成 List集合, 存每一行  
        /// 集合中放的是键值对字典,存每一列  
        /// </summary>  
        /// <param name="dt">数据表</param>  
        /// <returns>哈希表数组</returns>  
        public static List<Dictionary<string, object>> DataTableToList(DataTable dt)  
        {  
            List<Dictionary<string, object>> list  
                 = new List<Dictionary<string, object>>();  
            foreach (DataRow dr in dt.Rows)  
            {  
                Dictionary<string, object> dic = new Dictionary<string, object>();  
                foreach (DataColumn dc in dt.Columns)  
                {  
                    dic.Add(dc.ColumnName, dr[dc.ColumnName]);  
                }  
                list.Add(dic);  
            }  
            return list;  
        }  
        /// <summary>  
        /// 数据集转键值对数组字典  
        /// </summary>  
        /// <param name="dataSet">数据集</param>  
        /// <returns>键值对数组字典</returns>  
        public static Dictionary<string, List<Dictionary<string, object>>> DataSetToDic(DataSet ds)  
        {  
            Dictionary<string, List<Dictionary<string, object>>> result = new Dictionary<string, List<Dictionary<string, object>>>();  
            foreach (DataTable dt in ds.Tables)  
                result.Add(dt.TableName, DataTableToList(dt));  
            return result;  
        }  
        /// <summary>  
        /// 数据表转JSON  
        /// </summary>  
        /// <param name="dataTable">数据表</param>  
        /// <returns>JSON字符串</returns>  
        public static string DataTableToJSON(DataTable dt)  
        {  
            return ObjectToJSON(DataTableToList(dt));  
        }  
        /// <summary>  
        /// JSON文本转对象,泛型方法  
        /// </summary>  
        /// <typeparam name="T">类型</typeparam>  
        /// <param name="jsonText">JSON文本</param>  
        /// <returns>指定类型的对象</returns>  
        public static T JSONToObject<T>(string jsonText)  
            {  
                JavaScriptSerializer jss = new JavaScriptSerializer();  
                try  
                {  
                    return jss.Deserialize<T>(jsonText);  
                }  
                catch (Exception ex)  
                {  
                    throw new Exception(ex.Message);  
                }  
            }  
        /// <summary>  
        /// 将JSON文本转换为数据表数据  
        /// </summary>  
        /// <param name="jsonText">JSON文本</param>  
        /// <returns>数据表字典</returns>  
        public static Dictionary<string, List<Dictionary<string, object>>> TablesDataFromJSON(string jsonText)  
        {  
            return JSONToObject<Dictionary<string, List<Dictionary<string, object>>>>(jsonText);  
        }  
        /// <summary>  
        /// 将JSON文本转换成数据行  
        /// </summary>  
        /// <param name="jsonText">JSON文本</param>  
        /// <returns>数据行的字典</returns>  
        public static Dictionary<string, object> DataRowFromJSON(string jsonText)  
        {  
            return JSONToObject<Dictionary<string, object>>(jsonText);  
        }  
    }