0

I am using GET method (MCV4 / WEB-API/ VS 2010).

I want to return a response string in json format.

I have a string (any string) and want to convert it to json to be return as response).

How can I convert any string to JSON format:

string s = "{\"one\":\"a\", \"two\": \"2\"}";
Request.CreateResponse(HttpStatusCode.OK, <what shall i put here in order to return json of string s>);

Can I do something like this? :

string s = "{\"one\":\"a\", \"two\": \"2\"}";
Request.CreateResponse(HttpStatusCode.OK, s, "application/json");

I need to convert string, because I am using 3rd party tool, that send me string (and not json object). I don't understand what wrong, because json represented actually a long string - it just called json.

I don't know whether the response add " sign, because I am cheking that on advanced rest client plugin for chrome, and I see " sign before and after the string. Nevertheless, string I pass, shall be with " sign before and after.

Thanks :)

1

1 Answer 1

2

Generally you don't convert the object you plan to return to a particular format with Web API. The server will return the data in the requested format if it knows how, based on content negotiation. So your function signature should return a string, and Web API will take care of converting to XML or JSON as 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 you want to return a dictionary, then something like 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);
    }
Sign up to request clarification or add additional context in comments.

10 Comments

What you mean - I shall do: Request.CreateResponse(HttpStatusCode.OK, s); with contents-type = application/json? What I see that response return null string.
I am using mcv4 as web-api, inherit apicontroller class like this: public HttpResponseMessage Post([FromBody]string value) { s = " ... " return Request.CreateResponse(HttpStatusCode.OK, s, "application/json"); but the above return a string (with " at start and at end, and not json). }
@Eitan Try my updated code, particularly the last block. And no, do not specify the content type in your response. Let the framework handle that for you.
See my clarifications on your post.
I want to do something very generic (one code for all strings) - I just send json as string, and want to return as string (because the parser is elsewhere - not in C#).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.