我最近需要将数据表序列化为JSON。我们使用的是.Net 2.0,无法使用.Net 3.5中的JSON序列化程序。我想这肯定已经做过了,所以我在网上搜寻发现了很多 不同的 选项。其中一些依赖于其他库,我很难在此处推广使用。其他方法需要先转换为
List<Dictionary<>>
,这似乎有点笨拙和不必要。另一个方法将所有值视为字符串处理。出于某种原因,我不能真正支持它们中的任何一个,所以我决定自己动手写一个,如下所示。
从阅读//TODO
注释可以看出,代码还有几个地方不完整。这段代码已经在生产环境中使用了,因此基本上是可行的。它不完整的地方是我们知道我们的生产数据目前不会触及它(数据库中没有时间跨度或字节数组)。我在这里发布的原因是我觉得这可以再好一点,并且我想要帮助完成和改进这段代码。欢迎任何意见。
请注意,这种功能已经内置在 .Net 3.5 及更高版本中,因此今天只有在您仍受限于 .Net 2.0 的情况下才需要使用此代码。即使如此,JSON.Net 已成为这种操作的首选库。
public static class JSONHelper
{
public static string FromDataTable(DataTable dt)
{
string rowDelimiter = "";
StringBuilder result = new StringBuilder("[");
foreach (DataRow row in dt.Rows)
{
result.Append(rowDelimiter);
result.Append(FromDataRow(row));
rowDelimiter = ",";
}
result.Append("]");
return result.ToString();
}
public static string FromDataRow(DataRow row)
{
DataColumnCollection cols = row.Table.Columns;
string colDelimiter = "";
StringBuilder result = new StringBuilder("{");
for (int i = 0; i < cols.Count; i++)
{ // use index rather than foreach, so we can use the index for both the row and cols collection
result.Append(colDelimiter).Append("\"")
.Append(cols[i].ColumnName).Append("\":")
.Append(JSONValueFromDataRowObject(row[i], cols[i].DataType));
colDelimiter = ",";
}
result.Append("}");
return result.ToString();
}
// possible types:
// http://msdn.microsoft.com/en-us/library/system.data.datacolumn.datatype(VS.80).aspx
private static Type[] numeric = new Type[] {typeof(byte), typeof(decimal), typeof(double),
typeof(Int16), typeof(Int32), typeof(SByte), typeof(Single),
typeof(UInt16), typeof(UInt32), typeof(UInt64)};
// I don't want to rebuild this value for every date cell in the table
private static long EpochTicks = new DateTime(1970, 1, 1).Ticks;
private static string JSONValueFromDataRowObject(object value, Type DataType)
{
// null
if (value == DBNull.Value) return "null";
// numeric
if (Array.IndexOf(numeric, DataType) > -1)
return value.ToString(); // TODO: eventually want to use a stricter format. Specifically: separate integral types from floating types and use the "R" (round-trip) format specifier
// boolean
if (DataType == typeof(bool))
return ((bool)value) ? "true" : "false";
// date -- see https://weblogs.asp.net/bleroy/dates-and-json
if (DataType == typeof(DateTime))
return "\"\\/Date(" + new TimeSpan(((DateTime)value).ToUniversalTime().Ticks - EpochTicks).TotalMilliseconds.ToString() + ")\\/\"";
// TODO: add Timespan support
// TODO: add Byte[] support
//TODO: this would be _much_ faster with a state machine
//TODO: way to select between double or single quote literal encoding
//TODO: account for database strings that may have single \r or \n line breaks
// string/char
return "\"" + value.ToString().Replace(@"\", @"\\").Replace(Environment.NewLine, @"\n").Replace("\"", @"\""") + "\"";
}
}
更新:
这篇文章现在有点过时了,但我想指出有关代码如何处理日期的一些事情。我当时使用的格式是有道理的,原因在注释的url中说明。然而,该理由包括以下内容:
老实说,JSON Schema 通过将字符串“子类型化”为日期文字确实解决了问题,但这仍然是正在进行的工作,在任何显著的采用之前需要时间。
好吧,时间已经过去了。今天,只需使用ISO 8601日期格式即可。我不会费力更改代码,因为这真的很古老。直接使用 JSON.Net 就可以了。