C#无需第三方插件实现json和table互转

C# 数据库查询结果table转化为json字符串,或反向把json字符串转换为DataTable数据集合 以下代码经实践简单可用。


转换通用类定义:

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

namespace testcsoft
{

    public static class jsonObject
     {
     #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].ToString());
            }
            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
     
     }

}


使用方法:

DataTable -->  json :

string  json = jsonObject.ToJson(ds.Tables[0]);     

 // 其中  ds.Tables[0]  是数据库查询的表


  json -->  DataTable :

DataTable dt = jsonObject.ToDataTable(json);


这是测试从数据库获取数据并table和json互转结果

C#无需第三方插件实现json和table互转

上一篇:Windows 商店封杀 Chrome 和 Firefox 等第三方浏览器


下一篇:asp.net signalR 专题—— 第三篇 如何从外部线程访问 PersistentConnection