在服务器端获取JSON格式的数据

Get JSON formatted data on Server Side

本文关键字:数据 格式 JSON 服务器端 获取      更新时间:2023-09-26

我使用jquery AJAX将JSON字符串化数据传递为::

 $("#btnTest").click(function () {
        var models = [];
        models.push({ TaskID: 4, OwnerID: 1, RecurrenceID: null, Title: "Test Title213", Description: "Desc... "});
        $.ajax({
                type: 'POST',
                url:"/Home/Test_UpdateSchedule/",
                data: {models:JSON.stringify(models)},
                traditional: true,
                success: function (data)
                {
                    //
                }
        });
    });

在服务器端,我正试图将其访问为::

public ActionResult Test_UpdateSchedule(List<SchedulerViewModel> models)
{
         //Code Implementation
}

但我在服务器端没有得到数据。我将计数为0。

我的ViewModel为::

public class SchedulerViewModel 
    {
        public int TaskID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string RecurrenceRule { get; set; }
        public int? RecurrenceID { get; set; }
        public string RecurrenceException { get; set; }
        public bool IsAllDay { get; set; }
        public int? OwnerID { get; set; }
        public string StartTimezone { get; set; }
        public string EndTimezone { get; set; }
    }

如何在服务器端访问此处的数据?

只需更改JSON.stringify(),如下所示。

    JSON.stringify({ models: models })

现在工作的ajax帖子将是.

    $.ajax({
        type: 'POST',
        url: '@Url.Action("Test_UpdateSchedule", "Home")',
        data: JSON.stringify({ models: models }),
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            //
        }
    });

使用Org.json lib将json字符串转换为json对象

JSONObject jsonObj=新的JSONObject("您的参数字符串");

在覆盖到json对象之后,您可以在类中进行迭代和设置。