Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NETMVC。创建要转换为JSON对象的模型(具有动态名称属性)_C#_Json_Asp.net Mvc - Fatal编程技术网

C# NETMVC。创建要转换为JSON对象的模型(具有动态名称属性)

C# NETMVC。创建要转换为JSON对象的模型(具有动态名称属性),c#,json,asp.net-mvc,C#,Json,Asp.net Mvc,我需要生成Json输出,如下所示: { "01:30":{ "FREE":true, "PRICE":3500 }, "03:00":{ "FREE":true, "PRICE":2500 }, "13:30":{ "FREE":true, "PRICE":2500 } } 我这里的问题是时间字符串——它从一个条目到另一个条目的动态变化。我不知道如何

我需要生成Json输出,如下所示:

{
    "01:30":{
        "FREE":true,
        "PRICE":3500
     },
    "03:00":{
        "FREE":true,
        "PRICE":2500
    },
    "13:30":{
        "FREE":true,
        "PRICE":2500
    }
}
我这里的问题是时间字符串——它从一个条目到另一个条目的动态变化。我不知道如何构造将被序列化为正确json结果的c#类(模型)。 我的模型现在看起来像这样:

public class ScheduleViewModel
{
    public ScheduleInnerViewModel TIME { get; set; }
}

public class ScheduleInnerViewModel
{
    public bool FREE { get; set; }
    public int PRICE { get; set; }
}
输出结果为(错误):

我使用标准方法从控制器生成json结果:

List<ScheduleViewModel> model = new List<ScheduleViewModel>();
//fill the model...
return this.Json(model, JsonRequestBehavior.AllowGet);
列表模型=新列表();
//填充模型。。。
返回this.Json(model,JsonRequestBehavior.AllowGet);

尝试来自的不可接受的答案。它使用字典创建属性名(键),然后您可以使用ScheduleInnerViewModel作为值,例如:

 var obj = new Dictionary<string, ScheduleInnerViewModel>();
var obj=newdictionary();

尝试来自的不可接受的答案。它使用字典创建属性名(键),然后您可以使用ScheduleInnerViewModel作为值,例如:

 var obj = new Dictionary<string, ScheduleInnerViewModel>();
var obj=newdictionary();

如果有一个字典,其中键是字符串(时间戳),数据是模型类,那该怎么办

比如:

Dictionary dic=newdictionary();
添加(“01:30”,新的ScheduleInnerViewModel{Free=false,Price=100});
添加(“04:25”,新的ScheduleInnerViewModel{Free=false,Price=200});
添加(“13:00”,新的ScheduleInnerViewModel{Free=true,Price=0});
var json=JsonConvert.serialized对象(dic,新的JsonSerializerSettings{Formatting=Newtonsoft.json.Formatting.Indented});
Console.WriteLine(json);
输出:


如果有一个字典,其中键是字符串(时间戳),数据是模型类,那该怎么办

比如:

Dictionary dic=newdictionary();
添加(“01:30”,新的ScheduleInnerViewModel{Free=false,Price=100});
添加(“04:25”,新的ScheduleInnerViewModel{Free=false,Price=200});
添加(“13:00”,新的ScheduleInnerViewModel{Free=true,Price=0});
var json=JsonConvert.serialized对象(dic,新的JsonSerializerSettings{Formatting=Newtonsoft.json.Formatting.Indented});
Console.WriteLine(json);
输出:


“01:30”您需要一个变量来表示这个“01:30”您需要一个变量来表示这个似乎我迟到了一分钟:)似乎我迟到了一分钟:)谢谢!简单而优雅的解决方案。谢谢!简单而优雅的解决方案。
Dictionary<string, ScheduleInnerViewModel> dic = new Dictionary<string, ScheduleInnerViewModel>();

dic.Add("01:30", new ScheduleInnerViewModel{ Free = false, Price = 100 });
dic.Add("04:25", new ScheduleInnerViewModel{ Free = false, Price = 200 });
dic.Add("13:00", new ScheduleInnerViewModel{ Free = true, Price = 0 });

var json = JsonConvert.SerializeObject(dic, new JsonSerializerSettings { Formatting = Newtonsoft.Json.Formatting.Indented });

Console.WriteLine(json);