我可以转换成一个JSON字符串JsonResult? [英] Can I convert a JSON string into JsonResult?
问题描述
我存储在数据库中存储的一些字符串JSON,我想返回到客户端JsonResult。我知道,JSON(对象)将对象放入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
推荐答案
您不必返回 JsonResult
,因为它的工作是序列化的对象的成JSON字符串。你已经拥有的JSON字符串,所以才返回它在ContentResult类型,并指定正确的内容类型:
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" };
记住,你的MVC的操作方法都应该有的ActionResult
作为返回类型,所以你可以返回 ContentResult类型
刚一样容易 JsonResult
。
Remember that your MVC action methods should all have ActionResult
as a return type, so you can return ContentResult
just as easily as JsonResult
.
这篇关于我可以转换成一个JSON字符串JsonResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!