.net - how to create nested node C# -
i have developing json feed using sql database. have used below code project responce
public string convertdatatabletostring() { datatable dt = new datatable(); sqlconnection connection = new sqlconnection(configurationmanager.connectionstrings["dbconnection"].tostring()); sqlcommand command = new sqlcommand("select * table4", connection); sqldataadapter da = new sqldataadapter(command); da.fill(dt); javascriptserializer serializer = new javascriptserializer(); list<dictionary<string, object>> rows = new list<dictionary<string, object>>(); dictionary<string, object> row; foreach (datarow dr in dt.rows) { row = new dictionary<string, object>(); foreach (datacolumn col in dt.columns) { row.add(col.columnname, dr[col]); } rows.add(row); } return serializer.serialize(new { contacts = rows }); }
below json format got response above code
{ "contacts":[ { "username":"raja", "empid":"45" } ] }
but requirement below format
how add "employee" inside json.
{ "contacts":[ { "username":"raja", "employee":{ "empid":"40" } } ] }
my proposal:
- just after finishing
foreach (datarow dr in dt.rows) {}
try clone rows collection new 1 (not reference) - loop through newly created collection , modify suit needs
- serialize new collection , return it.
hope helps,
Comments
Post a Comment