程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#擴大datatable轉json示例

c#擴大datatable轉json示例

編輯:C#入門知識

c#擴大datatable轉json示例。本站提示廣大學習愛好者:(c#擴大datatable轉json示例)文章只能為提供參考,不一定能成為您想要的結果。以下是c#擴大datatable轉json示例正文



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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved