Home >Backend Development >C++ >How to Deserialize Complex JSON Objects into a Strongly-Typed List in an ASP.NET MVC Controller?

How to Deserialize Complex JSON Objects into a Strongly-Typed List in an ASP.NET MVC Controller?

Barbara Streisand
Barbara StreisandOriginal
2025-01-04 17:24:39906browse

How to Deserialize Complex JSON Objects into a Strongly-Typed List in an ASP.NET MVC Controller?

Deserializing Complex Objects with JSON and jQuery in an ASP.NET MVC Controller

In ASP.NET MVC, deserializing complex objects from JSON can be a challenge. This question addresses this issue, where the user attempts to pass an array of complex objects from jQuery to a controller action.

To address this, the solution leverages the [JsonFilter](https://web.archive.org/web/20120313075719/http://www.asp.net/web-api/overview/advanced/sending-and-receiving-json-in-aspnet-web-api) custom attribute. This attribute deserializes the JSON request into the appropriate type and binds it to an action parameter.

Updated View Code

// Serialize the results into a JSON object
var postData = { widgets: results };

// Send the JSON data to the controller
$.ajax({
    url: '/portal/Designer.mvc/SaveOrUpdate',
    type: 'POST',
    dataType: 'json',
    data: $.toJSON(widgets),
    contentType: 'application/json; charset=utf-8',
    success: function(result) {
        alert(result.Result);
    }
});

Modified Controller Code

[JsonFilter(Param = "widgets", JsonDataType = typeof(List<PageDesignWidget>))]
public JsonResult SaveOrUpdate(List<PageDesignWidget> widgets)
{
    // ... code to handle the updated widgets ...
}

Custom JsonFilter Attribute

public class JsonFilter : ActionFilterAttribute
{
    public string Param { get; set; }
    public Type JsonDataType { get; set; }
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.HttpContext.Request.ContentType.Contains("application/json"))
        {
            string inputContent;
            using (var sr = new StreamReader(filterContext.HttpContext.Request.InputStream))
            {
                inputContent = sr.ReadToEnd();
            }
            var result = JsonConvert.DeserializeObject(inputContent, JsonDataType);
            filterContext.ActionParameters[Param] = result;
        }
    }
}

This solution effectively deserializes the JSON array into a strongly-typed list within the controller, enabling the developer to manipulate complex objects with ease.

The above is the detailed content of How to Deserialize Complex JSON Objects into a Strongly-Typed List in an ASP.NET MVC Controller?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn