我想从C#代码编写一个json文件,就像下面的json文件看起来一样:
{
"Monday": [
{
"class": "Example",
"time": "09:00"
},
{
"class": "Example2",
"time": "09:50"
}
],
"Tuesday": [
{
"class": "Example3",
"time": "09:00"
},
{
"class": "Example4",
"time": "09:50"
}
]
}
EDIT这是我正在使用的课程:
public class Lesson {
public string title { get; set; }
public string timeStart { get; set; }
public string timeEnd { get; set; }
}
尽管我对数组一无所知,但我已经找到了将单个对象写入文件的方法。
如果解决方案显而易见,请事先打扰,我在浪费您的时间,但是我最近才开始使用json文件。
谢谢,
bezunyl
您可以:
Dictionary<string,List<Lesson>>
JsonConver.SerializeObject(dict)
。这里是如何与字典进行交互的许多可能方式的示例。
dict["Monday"]= new List<Lesson>();
dict["Monday"].Add(new Lesson(...));
When calling SerializeObject you can specify formatting as Indented: `JsonConvert.SerializeObject(o, Formatting.Indented);`