asp.net - 我可以将 JSON 字符串转换为 JsonResult 吗?

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

最佳答案

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

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

请记住,您的 MVC 操作方法都应该将 ActionResult 作为返回类型,因此您可以像返回 JsonResult 一样轻松地返回 ContentResult

https://stackoverflow.com/questions/2685155/

相关文章:

android - JSONException : Value of type java. lang

python - 如何在 Python 中将数字列表转换为 jsonarray

javascript - 这个简单的字符串是否被认为是有效的 JSON?

c# - 在 C# 中通过 POST 发送 JSON 并接收返回的 JSON?

javascript - 将 HTML 映射到 JSON

c# - 将日期时间从 javascript 传递给 c# (Controller)

c# - 使用 JsonConvert.DeserializeObject 将 Json 反序列化为

ruby-on-rails - 使用 Rspec,如何在 Rails 3.0.11 中测试我的 Co

json - JSON 有哪些好的 CLI 工具?

json - 如何对 Windows Azure 动态内容启用 gzip HTTP 压缩