简体   繁体   English

在ASP.NET MVC中将JSON格式化为数组

[英]Formatting JSON as an Array in ASP.NET MVC

I'm having a hard time in returning the formatted JSON as an array. 我很难将格式化的JSON作为数组返回。 My current code already returns JSON as an array but I want to get rid of these bracket first [] to get my desired output. 我当前的代码已将JSON作为数组返回,但是我想先删除这些括号[]以获得所需的输出。

Controller 调节器

public JsonResult GenerateGanttChart()
    {
        SPMS_GanttChartLayers ddl = new SPMS_GanttChartLayers();
        var data = ddl.GenerateGanttChart();
        return Json(data, JsonRequestBehavior.AllowGet);
    }

Layer

public IEnumerable<SPMS_GanttChartRootModel> GenerateGanttChart()
    {
        List<SPMS_GanttChartModel> child_data = new List<SPMS_GanttChartModel>();
        {
            using (SqlConnection con = new SqlConnection(Conn.MyConn()))
            {
                SqlCommand com = new SqlCommand("dbo.sp_SPMS_GanttChart 7078, 1", con);
                con.Open();
                SqlDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    SPMS_GanttChartModel value = new SPMS_GanttChartModel();
                    value.id = Convert.ToInt32(reader.GetValue(0));
                    value.start_date = Convert.ToString(reader.GetValue(1));
                    value.duration = Convert.ToInt32(reader.GetValue(2));
                    value.text = Convert.ToString(reader.GetValue(3));
                    child_data.Add(value);
                }
            }

        }
        List<SPMS_GanttChartRootModel> array = new List<SPMS_GanttChartRootModel>();
        {
            SPMS_GanttChartRootModel value = new SPMS_GanttChartRootModel();
            value.data = child_data;
            array.Add(value);
        };
        return array;
    }

Current Output 电流输出

[{
"data": [{
    "id": 1,
    "start_date": "11/07/2017 08:00:00 AM",
    "duration": 23,
    "text": "Project #1"
}, ... ]
}]

Desired Output 期望的输出

{
"data": [{
    "id": 1,
    "start_date": "11/07/2017 08:00:00 AM",
    "duration": 23,
    "text": "Project #1"
}, ... ]
}

Currently you are returning a List<SPMS_GanttChartRootModel> from your GenerateGanttChart method. 当前,您正在从GenerateGanttChart方法返回List<SPMS_GanttChartRootModel>

Return the SPMS_GanttChartRootModel object. 返回SPMS_GanttChartRootModel对象。

SPMS_GanttChartRootModel value = new SPMS_GanttChartRootModel();
value.data = child_data;
return value;

Also make sure your method's return type is SPMS_GanttChartRootModel now 还要确保方法的返回类型现在为SPMS_GanttChartRootModel

public SPMS_GanttChartRootModel  GenerateGanttChart()
{
    var child_data = new List<SPMS_GanttChartModel>();
    {
        //your existing code to populate this collection
    };

     var value = new SPMS_GanttChartRootModel();
     value.data = child_data;
     return value;
}

and in your action method, pass this single object to the Json method 然后在您的操作方法中,将此单个对象传递给Json方法

public JsonResult GenerateGanttChart()
{
    var ddl = new SPMS_GanttChartLayers();
    var data = ddl.GenerateGanttChart();
    return Json(data, JsonRequestBehavior.AllowGet);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM