我可以将 JSON 字符串转换为 JsonResult 吗?

发布于 2024-08-29 17:17:21 字数 130 浏览 3 评论 0原文

我在数据库中存储了一些存储的 JSON 字符串,我想将其作为 JsonResult 返回给客户端。我知道 Json(object) 将对象转换为 JsonResult 但如果我已经将结果存储在字符串中怎么办?我可以将其转换为 JsonResult

I have some stored JSON strings stored in the DB which I want to return to the client as JsonResult . I know that Json(object) turns an object into JsonResult but what if I already have the result in a string ? can I cast it to JsonResult

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

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

发布评论

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

评论(1

后来的我们 2024-09-05 17:17:21

您不需要返回JsonResult,因为它的工作是将对象序列化为JSON字符串。您已经有了 JSON 字符串,因此只需在 ContentResult 中返回它并指定正确的内容类型:

string json = //get some json from your DB
return new ContentResult { Content = json, ContentType = "application/json" };

请记住,您的 MVC 操作方法都应将 ActionResult 作为返回类型,以便您可以返回 ContentResult 就像 JsonResult 一样简单。

You don't need to return a JsonResult because its job is to serialize an object into JSON string. You already have the JSON string, so just return it in a ContentResult and specify the correct content type:

string json = //get some json from your DB
return new ContentResult { Content = json, ContentType = "application/json" };

Remember that your MVC action methods should all have ActionResult as a return type, so you can return ContentResult just as easily as JsonResult.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文