I'm looking for way to convert dataSet with relations between its tables to JSON string.
this is my code for dataset serializer, how can I make the tables to be nested (like in nested reapeter of asp.net?)
thanks,
Avital
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;
}