.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:

  1. just after finishing foreach (datarow dr in dt.rows) {} try clone rows collection new 1 (not reference)
  2. loop through newly created collection , modify suit needs
  3. serialize new collection , return it.

hope helps,


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -