首页 > 编程 > C# > 正文

c#扩展datatable转json示例

2020-01-24 02:41:07
字体:
来源:转载
供稿:网友

复制代码 代码如下:

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>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表