public static string DataSetToJSON(DataSet ds)
{
string JSON = "{\\"Tables\\": \[";
for (int i = 0; i < ds.Tables.Count; i++)
{
string tmp = DataTableToJSON(ds, "Rows", i);
JSON += tmp;
if (i < ds.Tables.Count - 1)
{
JSON += " , ";
}
}
JSON += "\]}";
return JSON;
}
public static string DataTableToJSON(DataSet ds, string dsName, int table)
{
string JSON = "{\\"" + dsName + "\\" : \[";
for (int i = 0; i < ds.Tables\[table\].Rows.Count; i++)
{
//add the row JSON bits...
JSON += "{";
for (int j = 0; j < ds.Tables\[table\].Rows\[i\].Table.Columns.Count; j++)
{
string columnName = ds.Tables\[table\].Rows\[i\].Table.Columns\[j\].ColumnName;
string columnValue = ds.Tables\[table\].Rows\[i\]\[j\].ToString();
//Replace any crlf's in the column value...
columnValue = columnValue.Replace("\\r\\n", ", ");
JSON += "\\"" + columnName + "\\": \\"" + columnValue + "\\"";
if (j != ds.Tables\[table\].Rows\[i\].Table.Columns.Count - 1)
{
JSON += " , ";
}
}
JSON += "}";
if (i != ds.Tables\[table\].Rows.Count - 1)
{
JSON += " , ";
}
}
JSON += "\]";
JSON += "}";
return JSON;
}
If it don't work, I don't care :)
"Now I guess I'll sit back and watch people misinterpret what I just said......" Christian Graus At The Soapbox