c extends the datatable to json example

  • 2020-06-23 01:45:18
  • OfStack


namespace PadWebServices.Model
{
   public static class DataTableExtender
    {
       public static string ToJson(this DataTable dt,string tbName)  // this DataTable  Identification of DataTable The expansion of the class 
            {
                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;
                }
            }

    }
}

When used, using extends the space where the class resides.


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

webservice here returns the same format as XML, only by converting to JSON's data, as in 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>

The data not converted to JSON is as follows:


<?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>


Related articles: