Converting any string to JSON format in C# -


i using method (mcv4 / web-api/ vs 2010).

i want return response string in json format.

i have string (any string) , want convert json return response).

how can convert string json format:

string s = "{\"one\":\"a\", \"two\": \"2\"}"; request.createresponse(httpstatuscode.ok, <what shall put here in order return json of string s>); 

can this? :

string s = "{\"one\":\"a\", \"two\": \"2\"}"; request.createresponse(httpstatuscode.ok, s, "application/json"); 

i need convert string, because using 3rd party tool, send me string (and not json object). don't understand wrong, because json represented long string - called json.

i don't know whether response add " sign, because cheking on advanced rest client plugin chrome, , see " sign before , after string. nevertheless, string pass, shall " sign before , after.

thanks :)

generally don't convert object plan return particular format web api. server return data in requested format if knows how, based on content negotiation. function signature should return string, , web api take care of converting xml or json appropriate.

see web api content negotiation.

update, example function:

public string getstring()      {      string s="hello, world!";      return s;      } 

or

public httpresponsemessage getstring()     {     string s="hello, world!";     return request.createresponse(httpstatuscode.ok, s);     } 

if want return dictionary, this:

public dictionary<string,string> getdict()     {     var dict=new dictionary<string,string>();     dict.add("one", "a");     dict.add("two", "2");     return dict;     } 

or

public httpresponsemessage getdict()     {     var dict=new dictionary<string,string>();     dict.add("one", "a");     dict.add("two", "2");     return request.createresponse(httpstatuscode.ok, dict);     } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -