日期:2014-05-17  浏览次数:20680 次

怎么将数据库中读取出来的数据变成josn格式的数据?
RT,请问有哪些方法
------最佳解决方案--------------------
1:自己拼接成json格式
2:JavaScriptSerializer 
3:DataContractJsonSerializer
2和3都要3.5才支持!
------其他解决方案--------------------
可以根据自己的数据源写一个转json的方法(自己拼接json格式)
------其他解决方案--------------------
http://www.cnblogs.com/Ferry/archive/2010/08/03/1791439.html

看到底
------其他解决方案--------------------
只能用拼接的方法吗?还有没有别的方法了?
------其他解决方案--------------------
数据源以DataSet为例

private string ConvertToJson(DataSet ds)
    {
        StringBuilder retVal = new StringBuilder();
        int TableCount = 0, RowCount = 0, ColumnCount = 0;
        retVal.Append("{");
        foreach (DataTable dt in ds.Tables)
        {
            TableCount++;
            retVal.AppendFormat("{0}:[", dt.TableName);
            foreach (DataRow row in dt.Rows)
            {
                RowCount++; retVal.Append("{");
                foreach (DataColumn column in dt.Columns)
                {
                    ColumnCount++; retVal.AppendFormat("{0}:\"{1}\"{2}", column.ColumnName, row[column].ToString().Trim().Replace("'", "\\'").Replace("\\r", "<br/>").Replace("\\n", "<br/>").Replace(System.Environment.NewLine, "<br/>"), ColumnCount == dt.Columns.Count ? "" : ",");
                } ColumnCount = 0; retVal.Append("}"); retVal.AppendFormat("{0}", RowCount == dt.Rows.Count ? "" : ",");
            }
            RowCount = 0;
            retVal.Append("]");
            retVal.AppendFormat(TableCount == ds.Tables.Count ? "" : ",");
        }
        retVal.Append("}");
        return retVal.ToString();
    }



------其他解决方案--------------------
。net中不是有一个josn序列化的类吗?这是干什么用的?
------其他解决方案--------------------
很多。。。搜下
------其他解决方案--------------------
自己接接,或者使用json序列化类