Export (0) Print
Expand All

JsonResult Class

Represents a class that is used to send JSON-formatted content to the response.

Namespace:  System.Web.Mvc
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)

public class JsonResult : ActionResult

The JsonResult type exposes the following members.

Public methodJsonResultInitializes a new instance of the JsonResult class.

Public propertyContentEncodingGets or sets the content encoding.
Public propertyContentTypeGets or sets the type of the content.
Public propertyDataGets or sets the data.
Public propertyJsonRequestBehaviorGets or sets a value that indicates whether HTTP GET requests from the client are allowed.
Public propertyMaxJsonLengthGets or sets the maximum length of data.
Public propertyRecursionLimitGets or sets the recursion limit.

Public methodEquals (Inherited from Object.)
Public methodExecuteResultEnables processing of the result of an action method by a custom type that inherits from the ActionResult class. (Overrides ActionResult.ExecuteResult(ControllerContext).)
Protected methodFinalize (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Protected methodMemberwiseClone (Inherited from Object.)
Public methodToString (Inherited from Object.)

You can return an instance of the JsonResult class by using the Json method.

The following example shows how to return an instance of the JsonResult class from an action method. The object that is returned specifies that a GET request is permitted.

public ActionResult Movies()
    var movies = new List<object>();

    movies.Add(new { Title = "Ghostbusters", Genre = "Comedy", Year = 1984 });
    movies.Add(new { Title = "Gone with Wind", Genre = "Drama", Year = 1939 });
    movies.Add(new { Title = "Star Wars", Genre = "Science Fiction", Year = 1977 });

    return Json(movies, JsonRequestBehavior.AllowGet);

The next example shows how to retrieve and display the JSON-formatted content.

<input name="btnGetMovies" id="btnGetMovies" type="submit" value="Get Movies">
<ul id="movieList"></ul>

<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
    $("#btnGetMovies").click(function () {
        var actionUrl = '@Url.Action("Movies", "Home")';
        $.getJSON(actionUrl, displayData);

    function displayData(response) {
        if (response != null) {
            for (var i = 0; i < response.length; i++) {
                $("#movieList").append("<li>" + response[i].Title + " " + response[i].Genre + " " + response[i].Year + "</li>")

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
© 2015 Microsoft