2016-11-18 32 views
0

我有門,看起來像一個列表:如何創建C#MVC JSON數組控制器

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陣列。

怎麼辦?

+0

你們是不是該數組序列化到JSON或列表轉換成數組? –

+0

將數組序列化爲Json – StrugglingCoder

回答

0

安裝JSON.net nuget包。使用JsonConvert.SerializeObject function

string jsonArray = JsonConvert.SerializeObject(doorsForSite);