Can I convert a JSON string into JsonResult?

2019-03-07 23:14发布

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

2条回答
老娘就宠你
2楼-- · 2019-03-07 23:42

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

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-03-07 23:44

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.

查看更多
登录 后发表回答