0

I wanna send JSON response to browser that requested based on REST service. I use something like this that includes some quotes in Controller Method:

return Json("blah\"blah", JsonRequestBehavior.AllowGet);

And I expect the result would be blah"blah But is blah\"blah and includes back slash too! I wanna have blah"blah in response without any conversion in client side. I know that need to perform this via C# codes but how to do that?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70

2 Answers2

3

C# and JSON encode characters similarly. The string blah"blah is encoded in both C# and JSON as "blah\"blah". It's perfectly expected, then, that your raw JSON includes the backslash.

When you decode that string with a proper JSON library, it again becomes the string blah"blah.

Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • can I send decoded version in json format to browser? – Amirhossein Mehrvarzi Apr 28 '14 at 20:29
  • @Amir you seem to be wanting a conflicting thing: the decoded version is `blah"blah`. If you send it as JSON, it's `"blah\"blah"`. Most likely, what you want to do is send it as JSON (which your code appears to be doing already) and then decode it on the client side, e.g. with `jQuery.parseJSON(myStr)` (if that's not automatically done). – Tim S. Apr 28 '14 at 20:36
0

I found the answer in two above threads:

  1. ASP.Net MVC: how to create a JsonResult based on raw Json Data
  2. How to output Json string as JsonResult in MVC4?

So that I need to use something like this:

return Content(jsonStringFromSomewhere, "application/json");

With this in mind considering that JSONP is used in case of ajax requesting to external service or URL. But I wanna build specific string due to parse with a special parser and I used JSON rather than JSONP and result was great.

Community
  • 1
  • 1
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70