怎么在C#中将Json转换成DataTable-创新互联
这期内容当中小编将会给大家带来有关怎么在C#中将Json转换成DataTable,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
创新互联建站坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站制作、网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的阿尔山网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!一、Json转换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 字符串 转换为 DataTable数据集合 /// /// ///public static DataTable ToDataTableTwo(string json) { DataTable dataTable = new DataTable(); //实例化 DataTable result; try { JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得大数值 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); //循环添加行到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 字符串 转换为 DataTable数据集合 /// /// ///public static DataTable ToDataTable(string json) { DataTable dataTable = new DataTable(); //实例化 DataTable result; try { JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得大数值 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.处理不规则Json,因为列并不确定,所以直接定义列,不动态生成
[{"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 字符串 转换为 DataTable数据集合 /// /// ///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; //取得大数值 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"; //内容:自动跳到下一单元格 } line = line.Substring(0, line.Length - 1) + "\n"; sw.Write(line); } sw.Close(); fs.Close(); }
三、调用实现,数据导出到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"); //调用函数 }
上述就是小编为大家分享的怎么在C#中将Json转换成DataTable了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
新闻名称:怎么在C#中将Json转换成DataTable-创新互联
本文路径:http://cdiso.cn/article/cshoep.html