c# - how to add a header and and footer constants when converting a sql table to a string value using JavaScriptSerializer -
can tells me how this?
i have function in c#:
public string convertlocationtabletostring() { int radius = 0; string locationtype = "marker"; datatable dt = new datatable(); using (sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["defaultconnection"].tostring())) { using (sqlcommand cmd = new sqlcommand("select lat=latitude, lng=longitude, locations", con)) { con.open(); sqldataadapter da = new sqldataadapter(cmd); da.fill(dt); system.web.script.serialization.javascriptserializer serializer = new system.web.script.serialization.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(rows); } } }
the table locations contains 2 rows latitude , lontitude values.
it produces string value of:
[{"lat":24.816925048828125,"lng":-107.37641906738281} , {"lat":24.815664291381836,"lng":-107.38169097900391}]
.
but want produce following:
[{"coordinates": [{"lat":24.816925048828125,"lng":-107.37641906738281}], "radius": 0,"locationtype": "marker"} ,{ "coordinates": [{lat":24.815664291381836,"lng":-107.38169097900391}],"radius": 0,"locationtype": "marker"}}]
please notice ‘radius’ , ‘locationtype’ not fields in table.
thank help.
rubenc
you serializing rows, returned table. if created object in c# matches desired output, looped on rows returned , set relevant items, serialize that.
in case, object consist of: - coordinates - type of list - radius - int assume - locationtype - guessing enum
then create arrray of these, , serialize array.
Comments
Post a Comment