c#擴(kuò)展datatable轉(zhuǎn)json示例
namespace PadWebServices.Model
{
public static class DataTableExtender
{
public static string ToJson(this DataTable dt,string tbName) // this DataTable 標(biāo)識(shí)對(duì)DataTable類的擴(kuò)展
{
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;
}
}
}
}
在用到的時(shí)候,using擴(kuò)展類所在的空間,就可以用了。
[WebMethod]
public string GetSwt_yBatch()
{
DataTable dt = new BllSwt_yBatch().GetSwt_yBatch();
return dt.ToJson("swt_yBatch");
}
這里的webservice返回的格式還是XML,只不過(guò)通過(guò)轉(zhuǎn)成JSON的數(shù)據(jù),在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>
沒(méi)有轉(zhuǎn)成JSON的數(shù)據(jù)是這樣:
<?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、Dataset轉(zhuǎn)Json數(shù)據(jù)
- C#實(shí)現(xiàn)Json轉(zhuǎn)DataTable并導(dǎo)出Excel的方法示例
- C#中DataTable 轉(zhuǎn)換為 Json的方法匯總(三種方法)
- C#實(shí)現(xiàn)DataTable,List和Json轉(zhuǎn)換的方法
- C#實(shí)現(xiàn)將json轉(zhuǎn)換為DataTable的方法
- C#中的DataSet、string、DataTable、對(duì)象轉(zhuǎn)換成Json的實(shí)現(xiàn)代碼
- C#中把Datatable轉(zhuǎn)換為Json的5個(gè)代碼實(shí)例
- C#中把Json數(shù)據(jù)轉(zhuǎn)為DataTable
相關(guān)文章
C#根據(jù)IP地址查詢所屬地區(qū)實(shí)例詳解
這篇文章主要介紹了C#根據(jù)IP地址查詢所屬地區(qū)實(shí)例詳解,調(diào)用的接口是免費(fèi)的接口,有需要的同學(xué)可以研究下2021-03-03
C#實(shí)現(xiàn)創(chuàng)建標(biāo)簽PDF文件的示例代碼
標(biāo)簽PDF文件包含描述文檔結(jié)構(gòu)和各種文檔元素順序的元數(shù)據(jù),是一種包含后端提供的可訪問(wèn)標(biāo)記,管理閱讀順序和文檔內(nèi)容表示的邏輯結(jié)構(gòu)的PDF文件。本文將用C#實(shí)現(xiàn)創(chuàng)建標(biāo)簽PDF文件,需要的可以參考一下2022-08-08
Unity實(shí)現(xiàn)OCR文字識(shí)別功能
這篇文章主要介紹了通過(guò)Unity接入百度AI接口,實(shí)現(xiàn)OCR文字識(shí)別功能,文中的實(shí)現(xiàn)步驟講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定的參考價(jià)值,需要的可以了解一下2022-01-01
C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法
這篇文章主要介紹了C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法,需要的朋友可以參考下2015-09-09
C# 執(zhí)行CMD命令并接收返回結(jié)果的操作方式
這篇文章主要介紹了C# 執(zhí)行CMD命令并接收返回結(jié)果的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04
C#實(shí)現(xiàn)將HTML網(wǎng)頁(yè)或HTML字符串轉(zhuǎn)換為PDF
將HTML轉(zhuǎn)換為PDF可實(shí)現(xiàn)格式保留、可靠打印、文檔歸檔等多種用途,滿足不同領(lǐng)域和情境下的需求,所以本文就來(lái)介紹一下如何使用C#實(shí)現(xiàn)將HTML網(wǎng)頁(yè)或HTML字符串轉(zhuǎn)換為PDF,有需要的可以參考下2024-01-01
DevExpress設(shè)置TreeList圖片節(jié)點(diǎn)背景色的方法
這篇文章主要介紹了DevExpress設(shè)置TreeList圖片節(jié)點(diǎn)背景色的方法,需要的朋友可以參考下2014-08-08
C#中LINQ多條件JOIN時(shí)為什么可以使用匿名類
這篇文章主要給大家介紹了關(guān)于C#中LINQ多條件JOIN時(shí)為什么可以使用匿名類的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧2018-09-09

