DataTable到JSON

时间:2009-01-16 18:28:33

标签: c# .net json .net-2.0 datatable

我最近需要将数据表序列化为JSON。我在哪里我们仍然在.Net 2.0,所以我不能在.Net 3.5中使用JSON序列化器。我认为这之前一定是这样做的,所以我去了网上看了found number different options。其中一些依赖于一个额外的库,我很难在这里推进。其他人需要先转换为List<Dictionary<>>,这似乎有点尴尬和不必要。另一个处理所有值,如字符串。出于某种原因,我无法真正落后于他们,所以我决定推出自己的,我发布在下面。

正如您从阅读//TODO评论中看到的那样,它在一些地方不完整。这段代码已经在生产中,所以它在基本意义上“起作用”。它不完整的地方是我们知道我们的生产数据当前不会达到它的地方(数据库中没有时间跨度或字节数组)。我在这里发帖的原因是我觉得这可能会更好一些,我希望能帮助完成和改进这段代码。欢迎任何投入。

请注意,此功能内置于.Net 3.5及更高版本中,因此今天使用此代码的唯一原因是,您仍然只能使用.Net 2.0。即便如此,JSON.Net已成为此类事物的goto库。

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 http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx
        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("\"", @"\""") + "\"";
    }
}

更新
这已经过时了,但我想指出一些关于这段代码如何处理日期的事情。我使用的格式在当时是有意义的,因为网址中的确切基本原理。但是,该理由包括以下内容:

  

说实话,JSON Schema通过将字符串“子类型化”为日期文字来解决问题,但这仍然在进行中,并且在达到任何重要的采用之前需要一些时间。

好吧,时间过去了。今天,可以使用ISO 8601日期格式。我不打算改变代码,因为真的:这很古老。只需使用JSON.Net。

3 个答案:

答案 0 :(得分:5)

如果它是微软的AJAX extensions for .NET 2.0,它会帮助你说服你的老板安装一个库吗?

其中包含System.Web.Script.Serialization.JavascriptSerializer,用于帖子中last link的第4步。

答案 1 :(得分:1)

我发现了这个:http://www.bramstein.com/projects/xsltjson/ 您可以将数据表转换为xml并使用xslt样式表将xml转换为json。

这是一种解决方法,而不是真正的解决方案。

答案 2 :(得分:1)

嘿budy,它就在Rick的博客文章Serializing DataTable using Json.NET中。他详细解释了如何使用Json.NET中的James Newton King来完成它。