c#扩展datatable转json示例

c#扩展datatable转json示例,第1张

概述复制代码代码如下:namespacePadWebServices.Model{  publicstaticclassDataTableExtender   {      publicstaticstringToJson(thisDataTabledt,stringtbNam

复制代码 代码如下:
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中是这样:

@L_502_2@ 代码如下:
<?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","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示例全部内容,希望文章能够帮你解决c#扩展datatable转json示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1255376.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存