private string GetSeperator(string myType)
{
return "\"";
}
private string ToJson(DataTable dt)
{
if (dt.Rows.Count == 0)
return "{}";
else
{
List<string> listCols = new List<string>();
List<string> listTypes = new List<string>();
for (int i = 0; i < dt.Columns.Count; i++)
{
listCols.Add(dt.Columns[i].ColumnName.ToLower());
listTypes.Add(dt.Columns[i].DataType.ToString());
}
string[] cols = listCols.ToArray();
string[] types = listTypes.ToArray();
MyCommon.WriteLog(string.Join(",", cols));
MyCommon.WriteLog(string.Join(",", types));
List<string> contents = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
List<string> myCols = new List<string>();
for (int j = 0; j < dt.Columns.Count; j++)
{
string separator = GetSeperator(types[j]);
myCols.Add(string.Format("\"{0}\":{1}"
, cols[j]
, separator + dt.Rows[i][j].ToString() + separator
));
}
contents.Add(string.Format("{{{0}}}"
, string.Join(",", myCols.ToArray())
));
}
return string.Format("[{0}]", string.Join(",", contents.ToArray()));
}
}