1
我正在尋找將dataSet與其表之間的關係轉換爲JSON字符串的方式。將數據集轉換爲與JSON字符串的關係
這是我的數據集串行代碼,我怎樣才能使這些表是嵌套(asp.net中的嵌套reapeter樣?)
感謝,
阿維塔爾
public static string DataTableToJSON(DataSet dt)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
string str = serializer.Serialize(ToDictionary(dt));
return str;
}
private static object RowsToDictionary(DataTable table)
{
var columns = table.Columns.Cast<DataColumn>().ToArray();
return table.Rows.Cast<DataRow>().Select(r => columns.ToDictionary(c => c.ColumnName, c => r[c].ToString().Trim().Replace("'", "\'")));
}
private static object RowsToDictionary(DataSet table)
{
return table.Tables.Cast<DataTable>().ToDictionary(t => t.TableName, t => RowsToDictionary(t));
}
private static Dictionary<string, object> ToDictionary(DataSet table)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
if (table != null)
dic.Add(table.DataSetName, RowsToDictionary(table));
return dic;
}
我試過但沒有工作。 –