Can I convert a JSON string into JsonResult?

左心房为你撑大大i 提交于 2019-12-03 00:53:30

问题


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


回答1:


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.




回答2:


You could return the string to the client and then use the $.parseJSON() (jquery) to parse it to an actual json object.



来源:https://stackoverflow.com/questions/2685155/can-i-convert-a-json-string-into-jsonresult

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!