MVC控制器中的嵌套Json数组

时间:2016-11-10 08:56:08

标签: c# arrays json asp.net-mvc

要问非常愚蠢的问题。但无法理解。

在C#MVC Controller操作中,我需要为测试目的建模Json数组。

但这显示了编译错误,而不是有效的Json

var result = {
    "controllerId": "controller1",
    "controllerName": "ControllerOne"
};

但这完全有效:

var scheduleResult = new[] 
{
   new { scheduleId = "schedule1",scheduleName = "scheduleOne"},
   new { scheduleId = "schedule2",scheduleName = "scheduleTwo"}
};

为什么会这样?

还有如何编写嵌套的Json数组:

我试过了:

var scheduleResult = new[] 
{
    new { scheduleId = "schedule1",scheduleName = "scheduleOne",new[]{ new {doorId="Door1",doorName="DoorOne"}, new { doorId = "Door2", doorName = "DoorTwo" } } },
    new { scheduleId = "schedule2",scheduleName = "scheduleTwo"}
};

但它显示语法错误。该怎么办?

我需要在该数组的每个元素中嵌套数组。

提前致谢。

3 个答案:

答案 0 :(得分:1)

嗯,C#不支持你写的方式。您不能只在C#中输入JSON,并且不幸的是它会工作。您可以尝试使用匿名类型:

 var result = new 
 {
     controllerId = "controller1",
     controllerName = "ControllerOne",
     myArray = new [] 
     {
          "a",
          "b"
     }
 };

如果因API调用而返回它,则转换为JSON没问题。

您正在谈论的嵌套数组不起作用,因为您需要为它们命名,您不能拥有没有名称的数组属性。见上面的例子。

答案 1 :(得分:0)

为什么不将Dictionary<TKey, TValue>Newtonsoft.Json一起使用?

简单的json:

IDictionary<string, string> obj = new Dictionary<string, string>();
obj.Add("controllerId", "controller1");
obj.Add("controllerName", "ControllerOne");

// {"controllerId":"controller1","controllerName":"ControllerOne"}
string json = JsonConvert.SerializeObject(obj);

嵌套json:

IList<string> obj = new List<string>();

IDictionary<string, string> first = new Dictionary<string, string>();
first.Add("scheduleId ", "schedule1");
first.Add("scheduleName", "scheduleOne");

IDictionary<string, string> second = new Dictionary<string, string>();
second.Add("scheduleId ", "schedule2");
second.Add("scheduleName", "scheduleTwo");

string first_json = JsonConvert.SerializeObject(first);
string second_json = JsonConvert.SerializeObject(second);

obj.Add(first_json);
obj.Add(second_json);

// ["{\"scheduleId \":\"schedule1\",\"scheduleName\":\"scheduleOne\"}","{\"scheduleId \":\"schedule2\",\"scheduleName\":\"scheduleTwo\"}"]
string json = JsonConvert.SerializeObject(obj);

答案 2 :(得分:0)

首先,我们应该创建具有相同返回类型模式的模型类

 public class ScheduleModel
    {
        public List<Schedule> ScheduleList { get; set; }
    }

    public class Schedule
    {
        public int ScheduleId { get; set; }
        public string ScheduleName { get; set; }
        public List<Door> DoorList { get; set; }
    }

    public class Door
    {
        public int DoorId { get; set; }
        public string DoorName { get; set; }
    }

现在在控制器Action创建测试数据

List<Door> doorList = new List<Door>();
            doorList.Add(new Door{DoorId = "Door1",DoorName = "DoorOne"});
            doorList.Add(new Door{DoorId = "Door2",DoorName = "DoorTwo"});

            List<Schedule> scheduleList = new List<Schedule>();
            scheduleList.Add(new Schedule{
                ScheduleId = "schedule1",
                ScheduleName = "scheduleOne",
                DoorList = doorList
            });

            scheduleList.Add(new Schedule
            {
                ScheduleId = "schedule2",
                ScheduleName = "scheduleTwo",
            });

            return Json(scheduleList, JsonRequestBehavior.AllowGet);

如果这个答案对您有利,请标记为答案。