欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#扩展datatable转json示例

程序员文章站 2024-02-12 09:31:43
复制代码 代码如下:namespace padwebservices.model{   public static class datatableext...

复制代码 代码如下:

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>