這期內(nèi)容當中小編將會給大家?guī)碛嘘P怎么在C#中將Json轉(zhuǎn)換成DataTable,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)建站堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都網(wǎng)站制作、網(wǎng)站建設、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的阿爾山網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!一、Json轉(zhuǎn)換DataTable
1.處理簡單Json:
[{"mac":"20:f1:7c:c5:cd:80","rssi":"-86","ch":"9"},{"mac":"20:f1:7c:c5:cd:85","rssi":"-91","ch":"9"}]
////// Json 字符串 轉(zhuǎn)換為 DataTable數(shù)據(jù)集合 /// /// ///public static DataTable ToDataTableTwo(string json) { DataTable dataTable = new DataTable(); //實例化 DataTable result; try { JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得大數(shù)值 ArrayList arrayList = javaScriptSerializer.Deserialize (json); if (arrayList.Count > 0) { foreach (Dictionary dictionary in arrayList) { if (dictionary.Keys.Count () == 0) { result = dataTable; return result; } //Columns if (dataTable.Columns.Count == 0) { foreach (string current in dictionary.Keys) { dataTable.Columns.Add(current, dictionary[current].GetType()); } } //Rows DataRow dataRow = dataTable.NewRow(); foreach (string current in dictionary.Keys) { dataRow[current] = dictionary[current]; } dataTable.Rows.Add(dataRow); //循環(huán)添加行到DataTable中 } } } catch { } result = dataTable; return result; }
2.處理復雜Json
[{"id":"00e58d51","data":[{"mac":"20:f1:7c:c5:cd:80","rssi":"-86","ch":"9"},{"mac":"20:f1:7c:c5:cd:85","rssi":"-91","ch":"9"}]},
{"id":"00e58d53","data":[{"mac":"bc:d1:77:8e:26:78","rssi":"-94","ch":"11"},{"mac":"14:d1:1f:3e:bb:ac","rssi":"-76","ch":"11"},{"mac":"20:f1:7c:d4:05:41","rssi":"-86","ch":"12"}]}]
////// Json 字符串 轉(zhuǎn)換為 DataTable數(shù)據(jù)集合 /// /// ///public static DataTable ToDataTable(string json) { DataTable dataTable = new DataTable(); //實例化 DataTable result; try { JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得大數(shù)值 ArrayList arrayList = javaScriptSerializer.Deserialize (json); if (arrayList.Count > 0) { foreach (Dictionary dictionary in arrayList) { if (dictionary.Keys.Count () == 0) { result = dataTable; return result; } //Columns if (dataTable.Columns.Count == 0) { foreach (string current in dictionary.Keys) { if (current != "data") dataTable.Columns.Add(current, dictionary[current].GetType()); else { ArrayList list = dictionary[current] as ArrayList; foreach (Dictionary dic in list) { foreach (string key in dic.Keys) { dataTable.Columns.Add(key, dic[key].GetType()); } break; } } } } //Rows string root = ""; foreach (string current in dictionary.Keys) { if (current != "data") root = current; else { ArrayList list = dictionary[current] as ArrayList; foreach (Dictionary dic in list) { DataRow dataRow = dataTable.NewRow(); dataRow[root] = dictionary[root]; foreach (string key in dic.Keys) { dataRow[key] = dic[key]; } dataTable.Rows.Add(dataRow); } } } } } } catch { } result = dataTable; return result; }
3.處理不規(guī)則Json,因為列并不確定,所以直接定義列,不動態(tài)生成
[{"id":"00e58d53","data":[{"mac":"34:b3:54:89:86:64","rssi":"-86","ch":"13"},{"mac":"50:bd:5f:02:80:44","rssi":"-90","ch":"1"}]},
{"id":"00ccda81","data":[{"mac":"bc:46:99:4e:96:c8","rssi":"-92","ch":"1"},{"mac":"bc:3a:ea:fc:77:6c","rssi":"-93","ch":"6","ds":"Y","essid":"vienna hotel WIFI"}]}]
////// Json 字符串 轉(zhuǎn)換為 DataTable數(shù)據(jù)集合 /// /// ///public static DataTable ToDataTable(string json) { DataTable dataTable = new DataTable(); //實例化 DataTable result; try { dataTable.Columns.Add("id"); dataTable.Columns.Add("mac"); dataTable.Columns.Add("rssi"); dataTable.Columns.Add("ch"); dataTable.Columns.Add("ts"); dataTable.Columns.Add("tmc"); dataTable.Columns.Add("tc"); dataTable.Columns.Add("ds"); dataTable.Columns.Add("essid"); JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得大數(shù)值 ArrayList arrayList = javaScriptSerializer.Deserialize (json); if (arrayList.Count > 0) { foreach (Dictionary dictionary in arrayList) { if (dictionary.Keys.Count () == 0) { result = dataTable; return result; }//Rows string root = ""; foreach (string current in dictionary.Keys) { if (current != "data") root = current; else { ArrayList list = dictionary[current] as ArrayList; foreach (Dictionary dic in list) { DataRow dataRow = dataTable.NewRow(); dataRow[root] = dictionary[root]; foreach (string key in dic.Keys) { dataRow[key] = dic[key]; } dataTable.Rows.Add(dataRow); } } } } } } catch { } result = dataTable; return result; }
二、導出Excel
////// 導出Excel /// /// /// public void dataTableToCsv(DataTable table, string file) { string title = ""; FileStream fs = new FileStream(file, FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(new BufferedStream(fs), System.Text.Encoding.Default); for (int i = 0; i < table.Columns.Count; i++) { title += table.Columns[i].ColumnName + "\t"; //欄位:自動跳到下一單元格 } title = title.Substring(0, title.Length - 1) + "\n"; sw.Write(title); foreach (DataRow row in table.Rows) { string line = ""; for (int i = 0; i < table.Columns.Count; i++) { line += row[i].ToString().Trim() + "\t"; //內(nèi)容:自動跳到下一單元格 } line = line.Substring(0, line.Length - 1) + "\n"; sw.Write(line); } sw.Close(); fs.Close(); }
三、調(diào)用實現(xiàn),數(shù)據(jù)導出到Excel
protected void Button1_Click(object sender, EventArgs e) { string str = File.ReadAllText(@"C:\Users\Admin\Desktop\json.txt"); DataTable dt = ToDataTable(str); this.dataTableToCsv(dt, @"E:\json.xls"); //調(diào)用函數(shù) }
上述就是小編為大家分享的怎么在C#中將Json轉(zhuǎn)換成DataTable了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)成都網(wǎng)站設計公司行業(yè)資訊頻道。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。