C# JSONHelper之Json转换方法大全

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Data;

/// <summary> 
/// JSON帮助类 
/// </summary> 
publicclassJSONHelper
{
    /// <summary> 
    /// 对象转JSON 
    /// </summary> 
    /// <param name="obj">对象</param> 
    /// <returns>JSON格式的字符串</returns> 
    publicstaticstringObjectToJSON(object obj)
    {
        JavaScriptSerializer jss =newJavaScriptSerializer();
        try
        {
            return jss.Serialize(obj);
        }
        catch(Exception ex)
        {

            thrownewException("JSONHelper.ObjectToJSON(): "+ ex.Message);
        }
    }

    /// <summary> 
    /// 数据表转键值对集合 www.2cto.com  
    /// 把DataTable转成 List集合, 存每一行 
    /// 集合中放的是键值对字典,存每一列 
    /// </summary> 
    /// <param name="dt">数据表</param> 
    /// <returns>哈希表数组</returns> 
    publicstaticList<Dictionary<string,object>>DataTableToList(DataTable dt)
    {
        List<Dictionary<string,object>> list
             =newList<Dictionary<string,object>>();

        foreach(DataRow dr in dt.Rows)
        {
            Dictionary<string,object> dic =newDictionary<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> 
    publicstaticDictionary<string,List<Dictionary<string,object>>>DataSetToDic(DataSet ds)
    {
        Dictionary<string,List<Dictionary<string,object>>> result =newDictionary<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> 
    publicstaticstringDataTableToJSON(DataTable dt)
    {
        returnObjectToJSON(DataTableToList(dt));
    }

    /// <summary> 
    /// JSON文本转对象,泛型方法 
    /// </summary> 
    /// <typeparam name="T">类型</typeparam> 
    /// <param name="jsonText">JSON文本</param> 
    /// <returns>指定类型的对象</returns> 
    publicstatic T JSONToObject<T>(string jsonText)
    {
        JavaScriptSerializer jss =newJavaScriptSerializer();
        try
        {
            return jss.Deserialize<T>(jsonText);
        }
        catch(Exception ex)
        {
            thrownewException("JSONHelper.JSONToObject(): "+ ex.Message);
        }
    }

    /// <summary> 
    /// 将JSON文本转换为数据表数据 
    /// </summary> 
    /// <param name="jsonText">JSON文本</param> 
    /// <returns>数据表字典</returns> 
    publicstaticDictionary<string,List<Dictionary<string,object>>>TablesDataFromJSON(string jsonText)
    {
        returnJSONToObject<Dictionary<string,List<Dictionary<string,object>>>>(jsonText);
    }

    /// <summary> 
    /// 将JSON文本转换成数据行 
    /// </summary> 
    /// <param name="jsonText">JSON文本</param> 
    /// <returns>数据行的字典</returns> 
    publicstaticDictionary<string,object>DataRowFromJSON(string jsonText)
    {
        returnJSONToObject<Dictionary<string,object>>(jsonText);
    }
}

没有更多推荐了,返回首页