public static string DataTable2Json(DataTable dt, bool bForceToDate = true)
{
StringBuilder builder = new StringBuilder();
builder.Append("[");
for (int i = 0; i < dt.Rows.Count; i++)
{
string str = "";
for (int j = 0; j < dt.Columns.Count; j++)
{
string str8;
if (str.Length > 0)
{
str = str + ",";
}
string str2 = "";
string name = dt.Columns[j].DataType.Name;
bool flag = ((((string.Compare(name, "Decimal", true) == 0) || (string.Compare(name, "Float", true) == 0)) || ((string.Compare(name, "Int16", true) == 0) || (string.Compare(name, "Int32", true) == 0))) || ((string.Compare(name, "Int64", true) == 0) || (string.Compare(name, "Double", true) == 0))) || (string.Compare(name, "Single", true) == 0);
if (name == "DateTime")
{
try
{
if (dt.Rows[i][j] != DBNull.Value)
{
DateTime time = Convert.ToDateTime(dt.Rows[i][j]);
if (((bForceToDate && (time.Hour == 0)) && (time.Minute == 0)) && (time.Second == 0))
{
str2 = time.ToString("yyyy-MM-dd");
}
else
{
str2 = time.ToString("yyyy-MM-dd HH:mm:ss");
}
}
}
catch
{
}
}
else
{
str2 = dt.Rows[i][j].ToString();
}
string strA = dt.Columns[j].ColumnName.Replace("\r", "").Replace("\n", "");
if (string.Compare(strA, "property_xml", true) == 0)
{
try
{
if (string.IsNullOrWhiteSpace(str2))
{
XmlDocument document = new XmlDocument();
document.LoadXml(str2);
XmlNodeList list = document.SelectNodes("table/tr/td");
for (int k = 0; k < list.Count; k += 2)
{
if ((str.Length > 0) && (k > 0))
{
str = str + ",";
}
string str5 = list[k].InnerText.Replace("\r", "").Replace("\n", "");
string str6 = list[k + 1].InnerText.Replace("\n", @"\n").Replace("\r", @"\r").Replace("\t", " ").Replace(@"\", @"\\").Replace("\"", "\\\"");
str8 = str;
str = str8 + "\"" + str5 + "\":\"" + str6 + "\"";
}
}
else
{
str = str + "\"" + strA + "\":\"\"";
}
}
catch (Exception)
{
str = str + "\"" + strA + "\":\"\"";
}
}
else
{
str2 = str2.Replace("\n", @"\n").Replace("\r", @"\r").Replace("\t", " ").Replace(@"\", @"\\").Replace("\"", "\\\"").Replace("\v", "");
if (flag)
{
if (string.IsNullOrWhiteSpace(str2))
{
str = str + "\"" + strA + "\":null";
}
else
{
str8 = str;
str = str8 + "\"" + strA + "\":" + str2;
}
}
else
{
str8 = str;
str = str8 + "\"" + strA + "\":\"" + str2 + "\"";
}
}
}
str = "{" + str + "}";
if (builder.Length > 1)
{
builder.Append(",");
}
builder.Append(str);
}
builder.Append("]");
return builder.ToString();
}