C# Json转换dataTable

 #region Json转换dataTable
    /// <summary>
    /// 将JArray转换为datatable
    /// </summary>
    /// <param name="dataArr"></param>
    /// <returns></returns>
    private DataTable JArrayToDatable(JArray dataArr)
    {
        if (dataArr == null || dataArr.Count <= 0) return null;

        DataTable result = new DataTable();
        var colnames = ((JObject)dataArr.First).Properties();
        List<string> columnNames = new List<string>();

        if (colnames == null || colnames.Count() <= 0) return null;

        foreach (var item in colnames)
        {
            if (!columnNames.Contains(item.Name))
            {
                columnNames.Add(item.Name);
            }
            result.Columns.Add(item.Name, typeof(string));
        }
        foreach (JObject data in dataArr)
        {
            JObject jo = JObject.Parse(data.ToString());
            DataRow row = result.NewRow();
            foreach (var columnName in columnNames)
            {
                if (jo.Property(columnName) == null)
                {
                    data.Add(columnName, "");
                    row[columnName] = data[columnName].ToString();
                }
                else
                {
                    row[columnName] = data[columnName].ToString();
                }
            }
            result.Rows.Add(row);
        }
        return result;
    }


    /// <summary>
    /// 将JArray转换为datatable
    /// </summary>
    /// <param name="dataArr"></param>
    /// <returns></returns>
    private DataTable JArrayToDatable(JArray dataArr)
    {
        if (dataArr == null || dataArr.Count <= 0) return null;

        DataTable result = new DataTable();
        var colnames = ((JObject)dataArr.First).Properties();
        List<string> columnNames = new List<string>();

        if (colnames == null || colnames.Count() <= 0) return null;

        foreach (var item in colnames)
        {
            if (!columnNames.Contains(item.Name))
            {
                columnNames.Add(item.Name);
            }
            if (item.Name == "ShowOrder")
            {
                result.Columns.Add(item.Name, typeof(int));
            }
            else
                result.Columns.Add(item.Name, typeof(string));
        }
        foreach (JObject data in dataArr)
        {
            JObject jo = JObject.Parse(data.ToString());
            DataRow row = result.NewRow();
            foreach (var columnName in columnNames)
            {
                if (jo.Property(columnName) == null)
                {
                    data.Add(columnName, "");
                    row[columnName] = data[columnName].ToString();
                }
                else
                {
                    row[columnName] = data[columnName].ToString();
                }
            }
            result.Rows.Add(row);
        }
        return result;
    }

    #endregion
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值