如何在c# MVC控制器中创建Json数组
本文关键字:创建 Json 数组 控制器 MVC | 更新日期: 2023-09-27 18:18:31
我有一个门的列表,看起来像:
DoorId DoorName ControllerId ControllerName
------ -------- ------------ --------------
Door1 DoorOne C1 C1
Door2 DoorTwo C1 C1
Door3 DoorThree C2 C2
我需要像这样格式化它:
var doorsForSite = new[]
{
new { ControllerId ="C1",ControllerName="C1",IsChecked = "false",
Doors = new[]
{
new { DoorId="Door1",DoorName="DoorOne",Schedules = scheduleList},
new { DoorId="Door2",DoorName="DoorTwo",Schedules = scheduleList},
}
},
new { ControllerId ="C2",ControllerName="C2",IsChecked = "false",
Doors = new[]
{
new { DoorId= "Door3",DoorName="DoorThree",Schedules = scheduleList}
}
}
};
即由ControllerId
和ControllerName
组成Doors
阵列。
怎么做?
安装JSON.net nuget包。使用JsonConvert。SerializeObject函数
string jsonArray = JsonConvert.SerializeObject(doorsForSite);