Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I convert a JSON string into 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

like image 745
xantrus Avatar asked Apr 21 '10 17:04

xantrus


People also ask

How can I convert JSON to string?

Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

What is JsonResult?

What is JsonResult ? JsonResult is one of the type of MVC action result type which returns the data back to the view or the browser in the form of JSON (JavaScript Object notation format). In this article we will learn about JsonResult by taking scenario to bind view using the JSON Data .

What is JsonRequestBehavior AllowGet?

If you need to send JSON in response to a GET, you'll need to explicitly allow the behavior by using JsonRequestBehavior. AllowGet as the second parameter to the Json method. However, there is a chance a malicious user can gain access to the JSON payload through a process known as JSON Hijacking.


1 Answers

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.

like image 128
Erv Walter Avatar answered Sep 20 '22 09:09

Erv Walter