ASP.Net MVC:如何基于原始 Json 数据创建 JsonResult

发布于 2024-09-28 23:57:15 字数 414 浏览 0 评论 0原文

有一个包含以下原始 Json 数据的 string(为了问题而简化):

  var MyString =  "{ 'val': 'apple' }";

如何创建表示 MyStringJsonResult 对象?

我尝试使用 Json(object) 方法。但它将原始 json 数据作为字符串处理 - 逻辑上:P-。所以返回的 HTTP 响应看起来像:

"{ 'val': 'apple' }"

而不是给定的原始 Json 数据:

{ 'val': 'apple' }

这就是我想要实现的

Having a string containing the following raw Json data (simplified for the sake of the question):

  var MyString =  "{ 'val': 'apple' }";

How can I create a JsonResult object representing MyString?

I tried to use the Json(object) method. but it handles the raw json data as an string -logically :P-. So the returned HTTP response looks like:

"{ 'val': 'apple' }"

instead of the given raw Json Data:

{ 'val': 'apple' }

this is what I want to achieve:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不知在何时 2024-10-05 23:57:15

Controller 上的 Json() 方法实际上是一个创建新 JsonResult 的辅助方法。如果我们查看 源此类的代码*,我们可以看到它并没有真正做那么多——只是将内容类型设置为application/json,序列化您的数据对象使用 JavaScriptSerializer,并将结果字符串写入其中。您可以通过从您的代码返回 ContentResult 来复制此行为(减去序列化,因为您已经这样做了)控制器代替。

public ActionResult JsonData(int id) {
    var jsonStringFromSomewhere = "{ 'val': 'apple' }";
    // Content() creates a ContentResult just as Json() creates a JsonResult
    return Content(jsonStringFromSomewhere, "application/json");
}

* 从 MVC2 开始,如果用户发出 HTTP GET 请求(而不是 POST),JsonResult 也会引发异常。允许用户使用 HTTP GET 检索 JSON 会产生安全隐患在您自己的应用程序中允许这样做之前,您应该了解这一点。

The Json() method on Controller is actually a helper method that creates a new JsonResult. If we look at the source code for this class*, we can see that it's not really doing that much -- just setting the content type to application/json, serializing your data object using a JavaScriptSerializer, and writing it the resulting string.. You can duplicate this behavior (minus the serialization, since you've already done that) by returning a ContentResult from your controller instead.

public ActionResult JsonData(int id) {
    var jsonStringFromSomewhere = "{ 'val': 'apple' }";
    // Content() creates a ContentResult just as Json() creates a JsonResult
    return Content(jsonStringFromSomewhere, "application/json");
}

* Starting in MVC2, JsonResult also throws an exception if the user is making an HTTP GET request (as opposed to say a POST). Allowing users to retrieve JSON using an HTTP GET has security implications which you should be aware of before you permit this in your own app.

江挽川 2024-10-05 23:57:15

我从字符串生成 json 数据的方法是在控制器中使用 JavaScriptResult

public JavaScriptResult jsonList( string jsonString)
{
   jsonString = "var jsonobject = new Array(" + jsonString + ");";
   return JavaScript(jsonString)
}

然后,当您请求将 json 字符串传递给控制器​​中的该操作时,结果将是一个带有 javascript 标头的文件。

The way I have generated json data from a string is by using JavaScriptResult in the controller:

public JavaScriptResult jsonList( string jsonString)
{
   jsonString = "var jsonobject = new Array(" + jsonString + ");";
   return JavaScript(jsonString)
}

Then when you request pass the json string to that action in your controller, the result will be a file with javascript headers.

橘寄 2024-10-05 23:57:15

我认为你可以为此使用 JavaScriptSerializer 类

var js = new System.Web.Script.Serialization.JavaScriptSerializer();
var jsonObject = js.Deserialize("{ 'val': 'apple' }", typeof(object));

I think you can use the JavaScriptSerializer class for this

var js = new System.Web.Script.Serialization.JavaScriptSerializer();
var jsonObject = js.Deserialize("{ 'val': 'apple' }", typeof(object));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文