可以考慮把dataset或則datatable轉成json,如把 datatable的資料轉成json:
///
/// Datatable轉換為Json
/// Datatable物件
public static string ToJson(DataTable dt)
{
StringBuilder jsonString = new StringBuilder();
if (dt.Rows.Count == 0)
jsonString.Append("[{}]");
return jsonString.ToString();
}
jsonString.Append("[");
DataRowCollection drc = dt.Rows;
for (int i = 0; i
jsonString.Append("{");
for (int j = 0; j
string strKey = dt.Columns[j].ColumnName;
string strValue = drc[i][j].ToString();
Type type = dt.Columns[j].DataType;
jsonString.Append("\"" + strKey + "\":");
strValue = StringFormat(strValue, type);
if (j
jsonString.Append(strValue + ",");
else
jsonString.Append(strValue);
jsonString.Append("},");
jsonString.Remove(jsonString.Length - 1, 1);
jsonString.Append("]");
可以考慮把dataset或則datatable轉成json,如把 datatable的資料轉成json:
///
/// Datatable轉換為Json
///
/// Datatable物件
///
public static string ToJson(DataTable dt)
{
StringBuilder jsonString = new StringBuilder();
if (dt.Rows.Count == 0)
{
jsonString.Append("[{}]");
return jsonString.ToString();
}
jsonString.Append("[");
DataRowCollection drc = dt.Rows;
for (int i = 0; i
{
jsonString.Append("{");
for (int j = 0; j
{
string strKey = dt.Columns[j].ColumnName;
string strValue = drc[i][j].ToString();
Type type = dt.Columns[j].DataType;
jsonString.Append("\"" + strKey + "\":");
strValue = StringFormat(strValue, type);
if (j
{
jsonString.Append(strValue + ",");
}
else
{
jsonString.Append(strValue);
}
}
jsonString.Append("},");
}
jsonString.Remove(jsonString.Length - 1, 1);
jsonString.Append("]");
return jsonString.ToString();
}