c# - how to Return json format in wcf -



c# - how to Return json format in wcf -

iam new wcf. iam developing wcf restful

iam returning string serializing json format using below code

public string convertdatatabletostring() { datatable dt = new datatable(); using (sqlconnection con = new sqlconnection("data source=mar-pc;database=user;user id=sa;password=123123;")) { using (sqlcommand cmd = new sqlcommand("select title=tname,tid=taddress userdetails userid='1'", 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); } homecoming serializer.serialize(rows); } } }

where iam getting result

"[{\"title\":\"sathyam\",\"tid\":\"nizamabad\"}]"

i want result

{"title":"sathyam","tid":"nizamabad"}

iam operation contract ,

[operationcontract] [webinvoke(method = "get", responseformat = webmessageformat.json, bodystyle = webmessagebodystyle.bare, uritemplate = "mygetdata/{value}/{password}")]

help me tried bodystyle wrapped no

you have square brakets besause serialize list of dictionaries, not single object.

maybe problem escape characters double-serialization: utilize responseformat = webmessageformat.json , serialize dictionary within convertion method. have on question: how homecoming clean json wcf service?

c# json wcf

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -