範例連接檔,需自行修改「紅色字」部分:
CSV檔:
https://societydata.taichung.gov.tw/System/DataSetDownload.aspx?idf=387120000J-000027&folder=RFXQ40397EQFQ49011&datadate=2015-07-01&format=CSV
JSON檔:
https://societydata.taichung.gov.tw/System/DataSetDownload.aspx?idf=387120000J-000027&folder=RFXQ40397EQFQ49011&datadate=2015-01-01&format=JSON
XML檔:
https://societydata.taichung.gov.tw/System/DataSetDownload.aspx?idf=387120000J-000027&folder=RFXQ40397EQFQ49011&datadate=2015-07-01&format=XML
這個範例只能用在「JSON、XML」為一層的狀態下。因為,JSON、XML可以很多層進行資料的編寫。
using System;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Xml;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
string s = "";
DataTable dt = new DataTable();
using (WebClient wc = new WebClient())
{
try
{
wc.Encoding = Encoding.UTF8;
string url = "https://societydata.taichung.gov.tw/System/DataSetDownload.aspx?idf=387120000J-000027&folder=RFXQ40397EQFQ49011&datadate=2015-07-01&format=CSV";
byte[] b = wc.DownloadData(url);
s = Encoding.UTF8.GetString(b);
dt = CsvStringToDataTable(s);
//dt = JsonStringToDataTable(s);
//dt = XmlStringToDataTable(s);
}
catch (WebException ex)
{
context.Response.ContentType = "text/plain";
context.Response.Write(ex.Message.ToString());
}
}
context.Response.ContentType = "text/plain";
context.Response.Write("資料數量:" + dt.Rows.Count);
============ function
/// <summary>
/// Json To DataTable
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public DataTable JsonStringToDataTable(string s)
{
var jsonLinq = JObject.Parse(s);
var srcArray = jsonLinq.Descendants().Where(d => d is JArray).First();
var trgArray = new JArray();
foreach (JObject row in srcArray.Children<JObject>())
{
var cleanRow = new JObject();
foreach (JProperty column in row.Properties())
{
if (column.Value is JValue)
{
cleanRow.Add(column.Name, column.Value);
}
}
trgArray.Add(cleanRow);
}
return JsonConvert.DeserializeObject<DataTable>(trgArray.ToString());
}
/// <summary>
/// XML To DataTable
/// </summary>
/// <param name="Xmlstring"></param>
/// <returns></returns>
public DataTable XmlStringToDataTable(string s)
{
XmlDocument Xmldoc = new XmlDocument();
Xmldoc.LoadXml(s);
XmlReader Xmlreader = XmlReader.Create(new System.IO.StringReader(Xmldoc.OuterXml));
DataSet ds = new DataSet();
ds.ReadXml(Xmlreader);
DataTable dt = ds.Tables[0];
return dt;
}
/// <summary>
/// CSV To DataTable
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static DataTable CsvStringToDataTable(string s)
{
DataTable dt = new DataTable();
string[] tableData = s.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
var col = from cl in tableData[0].Split(",".ToCharArray())
select new DataColumn(cl);
dt.Columns.AddRange(col.ToArray());
(from st in tableData.Skip(1)
select dt.Rows.Add(st.Split(",".ToCharArray()))).ToList();
return dt;
}
2019年9月2日
2014年5月3日
DataTable 轉 Json,利用ashx回傳入HTML5 C#
在寫 HTML5 時,因為,需要利用 ashx 回傳 Json 字串,但如果自已寫一個 function 是不是太不方便了?
ASP.NET,其實有一個 Newtonsoft.Json.dll 可以使用,但您的VS 2010、2012也許是沒有這個dll檔案,但我建立可以利用 NuGet 來下載
但NuGet又是如何安裝?我覺得可以參考以下的連結
http://blog.miniasp.com/post/2011/05/17/Useful-Visual-Studio-2010-tool-NuGet-Package-Manager.aspx
安裝完後,可以打開 工具 >> NuGet 套件管理員 >> 套件管理器主控台
打開「主控台」後,會出現下圖:
請在 PM> 後面輸入
Install-Package Newtonsoft.Json
這樣您的 Bin 就會自動加入 Newtonsoft.Json.dll
接下來,就可以利用下面的範例:回傳至 HTML5 Client 端
using System;
using System.Web;
/*Json.NET相關的命名空間,請使用NuGet*/
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class GetKS_TOWN : IHttpHandler {
public void ProcessRequest (HttpContext context) {
DBTools dbtools = new DBTools();
System.Data.DataTable getLandName = dbtools.GetKS_TOWN();
//將DataTable轉成JSON字串
string str_json = JsonConvert.SerializeObject(getLandName, Formatting.Indented);
context.Response.ContentType = "text/plain";
context.Response.Write(str_json);
}
public bool IsReusable {
get {
return false;
}
}
}
以上,試試看吧!
ASP.NET,其實有一個 Newtonsoft.Json.dll 可以使用,但您的VS 2010、2012也許是沒有這個dll檔案,但我建立可以利用 NuGet 來下載
但NuGet又是如何安裝?我覺得可以參考以下的連結
http://blog.miniasp.com/post/2011/05/17/Useful-Visual-Studio-2010-tool-NuGet-Package-Manager.aspx
安裝完後,可以打開 工具 >> NuGet 套件管理員 >> 套件管理器主控台
打開「主控台」後,會出現下圖:
請在 PM> 後面輸入
Install-Package Newtonsoft.Json
這樣您的 Bin 就會自動加入 Newtonsoft.Json.dll
接下來,就可以利用下面的範例:回傳至 HTML5 Client 端
using System;
using System.Web;
/*Json.NET相關的命名空間,請使用NuGet*/
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class GetKS_TOWN : IHttpHandler {
public void ProcessRequest (HttpContext context) {
DBTools dbtools = new DBTools();
System.Data.DataTable getLandName = dbtools.GetKS_TOWN();
//將DataTable轉成JSON字串
string str_json = JsonConvert.SerializeObject(getLandName, Formatting.Indented);
context.Response.ContentType = "text/plain";
context.Response.Write(str_json);
}
public bool IsReusable {
get {
return false;
}
}
}
以上,試試看吧!
訂閱:
文章 (Atom)

