如何输出JSON字符串作为JsonResult在MVC4? [英] How to output Json string as JsonResult in MVC4?

查看:441
本文介绍了如何输出JSON字符串作为JsonResult在MVC4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这看起来很简单,我必须要过思考它。

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

TL; DR;

我如何修改下面的code返回字符串中包含的,而不是碰巧包含JSON字符串JSON对象?

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);
}

这code返回常量,其中包含JSON字符串:

This code returns a string literal containing the json:

"{ success: "true" }"

不过,我想它返回字符串中包含的JSON的:

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

{ success: "true" }

稍长版本

我想快速原型一些外部API调用,只是想通过我的API的结果,现在假的响应。 JSON对象是不平凡 - 10000行或90KB的顺序上的东西。我不想做一个强类型的对象(S)为这一个JSON响应的所有内容只是这样我就可以通过解串器运行它 - 这样是出于

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. 呼叫externall API

  2. Web请求到一个变种的存储字符串结果(见上文json_string)

  3. 使用JsonResult生产方法JSON()输出这些结果为JSON(不是字符串)

任何帮助pciated大大AP $ P $ ...心灵融化。

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

推荐答案

JSON()的辅助方法的要点是序列化的JSON。

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");

这篇关于如何输出JSON字符串作为JsonResult在MVC4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆