c# - How to get Json data in web service without xml and string? -
i'm using angularjs need output data in json format. have tried web service given below getting data database. in wen service i'm getting output xml , string. need without xml , string.
here code:
[webmethod] public string getdatetimeformats() { datatable dt = new datatable(); string strquery = null; strquery = "sql query"; using (system.data.sqlclient.sqlconnection conn = new system.data.sqlclient.sqlconnection()) { conn.connectionstring = system.configuration.configurationmanager.appsettings["constr"]; using (system.data.sqlclient.sqlcommand cmd = new system.data.sqlclient.sqlcommand()) { cmd.commandtext = strquery; cmd.connection = conn; conn.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 = default(dictionary<string, object>); 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); } } }
install newtonsoft.json package. , try code.
code
public class helloworlddata { public string message; } [webmethod] public void getemployee() { javascriptserializer js = new javascriptserializer(); context.response.clear(); context.response.contenttype = "application/json"; helloworlddata data = new helloworlddata(); string sql = "sql_querty"; sqldataadapter da = new sqldataadapter(sql, system.configuration.configurationmanager.appsettings["connection_string"]); dataset ds = new dataset(); da.fill(ds); context.response.write(jsonconvert.serializeobject(ds, newtonsoft.json.formatting.indented)); }
Comments
Post a Comment