c#扩展datatable转json示例

时间:2022-08-19 10:44:55 


namespace PadWebServices.Model
{
   public static class DataTableExtender
    {
       public static string ToJson(this DataTable dt,string tbName)  // this DataTable 标识对DataTable类的扩展
            {
                StringBuilder JsonString = new StringBuilder();
                if (dt != null && dt.Rows.Count > 0)
                {
                    JsonString.Append("{ ");
                    JsonString.Append("\""+tbName+"\":[ ");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        JsonString.Append("{ ");
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            if (j < dt.Columns.Count - 1)
                            {
                                JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
                            }
                            else if (j == dt.Columns.Count - 1)
                            {
                                JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
                            }
                        }

                        /*end Of String*/
                        if (i == dt.Rows.Count - 1)
                        {
                            JsonString.Append("} ");
                        }
                        else
                        {
                            JsonString.Append("}, ");
                        }
                    }
                    JsonString.Append("]}");
                    return JsonString.ToString();
                }
                else
                {
                    return null;
                }

            }

    }
}

在用到的时候,using扩展类所在的空间,就可以用了。


[WebMethod]
public string GetSwt_yBatch()
{   
    DataTable dt = new BllSwt_yBatch().GetSwt_yBatch();
    return dt.ToJson("swt_yBatch");
}

这里的webservice返回的格式还是XML,只不过通过转成JSON的数据,在XML中是这样:


<?xml version="1.0" encoding="utf-8" ?>
  <string xmlns="http://tempuri.org/">{ "swt_yBatch":[ { "sDoc_c":"test1","sDocAdd_c":"test2","nBatch":"1"},
{ "sDoc_c":"test3","sDocAdd_c":"test4","nBatch":"2"}, { "sDoc_c":"test3","sDocAdd_c":"test4","nBatch":"2"} ]}</string>

没有转成JSON的数据是这样:


<?xml version="1.0" encoding="utf-8" ?>
- <DataSet xmlns="http://tempuri.org/">
- <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
- <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
- <xs:complexType>
- <xs:choice minOccurs="0" maxOccurs="unbounded">
- <xs:element name="Table1">
- <xs:complexType>
- <xs:sequence>
  <xs:element name="sDoc_c" type="xs:string" minOccurs="0" />
  <xs:element name="sDocAdd_c" type="xs:string" minOccurs="0" />
  <xs:element name="nBatch" type="xs:int" minOccurs="0" />
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:choice>
  </xs:complexType>
  </xs:element>
  </xs:schema>
- <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
- <NewDataSet xmlns="">
- <Table1 diffgr:id="Table11" msdata:rowOrder="0">
  <sDoc_c>test1</sDoc_c>
  <sDocAdd_c>test2</sDocAdd_c>
  <nBatch>1</nBatch>
  </Table1>
- <Table1 diffgr:id="Table12" msdata:rowOrder="1">
  <sDoc_c>test3</sDoc_c>
  <sDocAdd_c>test4</sDocAdd_c>
  <nBatch>2</nBatch>
  </Table1>
- <Table1 diffgr:id="Table13" msdata:rowOrder="2">
  <sDoc_c>test3</sDoc_c>
  <sDocAdd_c>test4</sDocAdd_c>
  <nBatch>2</nBatch>
  </Table1>
  </NewDataSet>
  </diffgr:diffgram>
  </DataSet>

标签:c#,datatable,json
0
投稿

猜你喜欢

  • java数据结构实现机器人行走

    2023-12-04 00:44:24
  • Android完整Socket解决方案

    2023-12-22 14:42:40
  • java中分组统计的三种实现方式

    2023-05-29 06:04:13
  • Java线程优先级和守护线程原理解析

    2023-03-27 16:45:30
  • 快速了解hibernate配置文件与映射文件

    2023-11-04 23:02:26
  • SpringBoot接口加密解密统一处理

    2023-04-12 19:42:04
  • Java 替换word文档文字并指定位置插入图片

    2023-08-12 22:02:25
  • 浅谈SpringMVC的执行流程

    2023-09-30 17:59:59
  • C# Soap调用WebService的实例

    2021-06-18 20:27:08
  • android 仿微信demo——微信消息界面实现(移动端)

    2021-08-01 11:37:26
  • Maven Repository仓库的具体使用

    2021-11-04 21:33:34
  • JDK1.8中的ConcurrentHashMap源码分析

    2023-11-27 06:02:32
  • c# Newtonsoft 六个值得使用的特性(下)

    2022-02-18 11:38:32
  • Nginx启用压缩及开启gzip 压缩的方法

    2021-09-20 21:19:25
  • C#通过委托调用Button单击事件的方法

    2022-11-29 22:09:15
  • Andriod studio 打包aar 的方法

    2023-12-13 19:32:36
  • 详解mybatis插入数据后返回自增主键ID的问题

    2021-09-04 19:08:00
  • Android扩大View点击范围的方法

    2022-07-21 17:49:29
  • java如何连续执行多条cmd命令

    2023-07-13 13:10:41
  • Java中BigInteger与BigDecimal类用法总结

    2021-09-14 07:15:26
  • asp之家 软件编程 m.aspxhome.com