33

This seems so simple I must be over-thinking it.

TL;DR;

How can I modify the code below to return the json object contained in the string rather than a string that happens to contain json?

public ActionResult Test()
{
  var json_string = "{ success: \"true\" }";
  return Json(json_string, JsonRequestBehavior.AllowGet);
}

This code returns a string literal containing the json:

"{ success: "true" }"

However, I'd like it to return the json contained in the string:

{ success: "true" }

Slightly longer version

I'm trying to quickly prototype some external api calls and just want to pass those results through my "api" as a fake response for now. The json object is non-trivial - something on the order of 10,000 "lines" or 90KB. I don't want to make a strongly typed object(s) for all the contents of this one json response just so I can run it through a deserializer - so that is out.

So the basic logic in my controller is:

  1. Call externall api
  2. Store string result of web request into a var (see json_string above)
  3. Output those results as json (not a string) using the JsonResult producing method Json()

Any help is greatly appreciated... mind is melting.

longda
  • 10,153
  • 7
  • 46
  • 66
  • Just found the answer in the "related" questions sidebar of my own question: http://stackoverflow.com/a/3991940/298758 **This question (mine) is nearly a duplicate** – longda Aug 23 '13 at 18:54

2 Answers2

54

The whole point of the Json() helper method is to serialize as JSON.

If you want to return raw content, do that directly:

return Content(jsonString, "application/json");
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
46
public ActionResult Test()
{
  return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}
Andy T
  • 10,223
  • 5
  • 53
  • 95