So the "s it's put on the start and finish and the escaped "s inside aren't what I want, I just want it to be sent down literally.
I know its the return type of string that doing all this but is there another return type suitable for sending back a string that is already json data and want it to be sent down literally.
I know the return type could be strongly typed against a collection of objects but I want to send back a mixture of different types of objects.
What's happening is the return type string is being serialized to a JSON string. Instead, build the response directly. That skips the content-negotiation and serialization step.
public HttpResponseMessage Get()
{
var resp = new HttpResponseMessage()
{
Content = new StringContent("[{\"Name\":\"ABC\"},[{\"A\":\"1\"},{\"B\":\"2\"},{\"C\":\"3\"}]]")
};
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return resp;
}
Edit: As Chris Thomas said, you can also return a JObject. That's probably a better approach than building up a JSON string by hand. But if you are getting the JSON string from somewhere else, the above code would work.
[HttpGet]
public HttpResponseMessage jsonTest()
{
var resp = new HttpResponseMessage()
{
Content = new StringContent("[{\"Name\":\"ABC\"},[{\"A\":\"1\"},{\"B\":\"2\"},{\"C\":\"3\"}]]")
};
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return resp;
}
When i look it fiddler I get a 504 error :
ReadResponse() failed: The server did not return a response for this request.
I had come up with a work around anyway before reading your reply where I let the single string go down to the client in json then on the client do : jsData = $.parseJSON(data); which parses that single string into a proper js variable of arrays and objects.
StringContent also has an overload that takes an Encoding and a media type string:
var resp = new HttpResponseMessage { Content = new StringContent("[{\"Name\":\"ABC\"},[{\"A\":\"1\"},{\"B\":\"2\"},{\"C\":\"3\"}]]", System.Text.Encoding.UTF8, "application/json") };
I've found that constructor overload useful in the past.
Puma613
Member
216 Points
65 Posts
Web api method return json in string
Sep 19, 2012 03:41 PM|LINK
In a web api method im trying to return a string of ready prepared json :
[HttpGet] public string jsonTest() { return "[{\"Name\":\"ABC\"},[{\"A\":\"1\"},{\"B\":\"2\"},{\"C\":\"3\"}]]"; }But what actually gets sent back to the client when I look in fiddler is :
"[{\"Name\":\"ABC\"},[{\"A\":\"1\"},{\"B\":\"2\"},{\"C\":\"3\"}]]"
So the "s it's put on the start and finish and the escaped "s inside aren't what I want, I just want it to be sent down literally.
I know its the return type of string that doing all this but is there another return type suitable for sending back a string that is already json data and want it to be sent down literally.
I know the return type could be strongly typed against a collection of objects but I want to send back a mixture of different types of objects.
ChrisThomas
Member
65 Points
31 Posts
Re: Web api method return json in string
Sep 19, 2012 05:03 PM|LINK
You could create and populate a JObject (from JSON.Net) and return that with your custom properties.
I haven't tried it out myself, but it should work, or at least point you in the right direction.
MikeWasson
Member
486 Points
78 Posts
Microsoft
Re: Web api method return json in string
Sep 19, 2012 05:28 PM|LINK
What's happening is the return type string is being serialized to a JSON string. Instead, build the response directly. That skips the content-negotiation and serialization step.
public HttpResponseMessage Get() { var resp = new HttpResponseMessage() { Content = new StringContent("[{\"Name\":\"ABC\"},[{\"A\":\"1\"},{\"B\":\"2\"},{\"C\":\"3\"}]]") }; resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return resp; }Edit: As Chris Thomas said, you can also return a JObject. That's probably a better approach than building up a JSON string by hand. But if you are getting the JSON string from somewhere else, the above code would work.
Puma613
Member
216 Points
65 Posts
Re: Web api method return json in string
Sep 20, 2012 09:21 AM|LINK
Thanks, ive changed my function to this :
[HttpGet] public HttpResponseMessage jsonTest() { var resp = new HttpResponseMessage() { Content = new StringContent("[{\"Name\":\"ABC\"},[{\"A\":\"1\"},{\"B\":\"2\"},{\"C\":\"3\"}]]") }; resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return resp; }When i look it fiddler I get a 504 error :
ReadResponse() failed: The server did not return a response for this request.
I had come up with a work around anyway before reading your reply where I let the single string go down to the client in json then on the client do : jsData = $.parseJSON(data); which parses that single string into a proper js variable of arrays and objects.
panesofglass
Member
730 Points
237 Posts
Re: Web api method return json in string
Jan 21, 2013 12:46 PM|LINK
StringContent also has an overload that takes an Encoding and a media type string:
var resp = new HttpResponseMessage { Content = new StringContent("[{\"Name\":\"ABC\"},[{\"A\":\"1\"},{\"B\":\"2\"},{\"C\":\"3\"}]]", System.Text.Encoding.UTF8, "application/json") };I've found that constructor overload useful in the past.