The Problem:
Had a hard time returning a plain JSON object from an ApiController (MVC4). Instead, I'm getting the JSON object as a string. For example:
"{\"dobSP\":\"90.25\",\"latestSP\":\"2025.9\",\"dobStampPrice\":\"0.15\",\"latestStampPrice\":\"0.49\",\"dobStampImage\":\"http://media1.s-nbcnews.com/j/msnbc/Components/Photos/051207/051207_stamp_news_hmed.grid-4x2.jpg\",\"latestStampImage\":\"http://media1.s-nbcnews.com/j/msnbc/Components/Photos/051207/051207_stamp_news_hmed.grid-4x2.jpg\",\"dataUpdated\":\"1/8/2015 11:27:48 AM\"}"
What I've Learned:
My Get method (in the controller) was specified to return a string object. What I needed to return was a HttpResponseMessage type.
The Fix:
I simply created a method that created the response for me:
private HttpResponseMessage ConvertToHttpResponse(string text)
{
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(text, Encoding.UTF8, "application/json");
return response;
}
When returning the value from my Get method, I simply added this (using JSON.net to serialize):
return ConvertToHttpResponse(JsonConvert.SerializeObject(payload));
And, here's the result I wanted:
Hope this helps somebody
More Reading:
http://stackoverflow.com/questions/23227952/apicontroller-to-return-json-string-as-is
http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage%28v=vs.118%29.aspx
Had a hard time returning a plain JSON object from an ApiController (MVC4). Instead, I'm getting the JSON object as a string. For example:
"{\"dobSP\":\"90.25\",\"latestSP\":\"2025.9\",\"dobStampPrice\":\"0.15\",\"latestStampPrice\":\"0.49\",\"dobStampImage\":\"http://media1.s-nbcnews.com/j/msnbc/Components/Photos/051207/051207_stamp_news_hmed.grid-4x2.jpg\",\"latestStampImage\":\"http://media1.s-nbcnews.com/j/msnbc/Components/Photos/051207/051207_stamp_news_hmed.grid-4x2.jpg\",\"dataUpdated\":\"1/8/2015 11:27:48 AM\"}"
What I've Learned:
My Get method (in the controller) was specified to return a string object. What I needed to return was a HttpResponseMessage type.
The Fix:
I simply created a method that created the response for me:
private HttpResponseMessage ConvertToHttpResponse(string text)
{
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(text, Encoding.UTF8, "application/json");
return response;
}
When returning the value from my Get method, I simply added this (using JSON.net to serialize):
return ConvertToHttpResponse(JsonConvert.SerializeObject(payload));
And, here's the result I wanted:
{"dobSP":"90.25","latestSP":"2025.9","dobStampPrice":"0.15","latestStampPrice":"0.49","dobStampImage":"http://media1.s-nbcnews.com/j/msnbc/Components/Photos/051207/051207_stamp_news_hmed.grid-4x2.jpg","latestStampImage":"http://media1.s-nbcnews.com/j/msnbc/Components/Photos/051207/051207_stamp_news_hmed.grid-4x2.jpg","dataUpdated":"1/8/2015 11:27:48 AM"}
Hope this helps somebody
More Reading:
http://stackoverflow.com/questions/23227952/apicontroller-to-return-json-string-as-is
http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage%28v=vs.118%29.aspx
Comments
Post a Comment