可以帮我如何在C#中创建Json文件,如下面的示例所示?我有中心列表,房间内部和内部的房间列表是预订列表(预订日期是预订的要素之一)。我不知道如何从列表中创建此数据结构。
"data": [
{
"meetingCentre": "EBC-MC_C7",
"meetingRoom": "EBC-C7-MR:1_1",
"reservations": {
"28.10.2016": [
{
"from": "10:00",
"to": "11:30",
"expectedPersonsCount": 4,
"customer": "College",
"videoConference": false,
"note": ""
},
{
"from": "12:00",
"to": "13:30",
"expectedPersonsCount": 4,
"customer": "College",
"videoConference": false,
"note": ""
}
],
"29.10.2016": [
{
"from": "10:00",
"to": "11:30",
"expectedPersonsCount": 4,
"customer": "College",
"videoConference": false,
"note": ""
}
]
}
},
....
答案 0 :(得分:1)
实际上,我认为这取决于用例是什么。 如果要将其用作DTO,则应考虑使用词典进行预订收集。
// Component
export class LandingYoutubeComponent implements OnInit {
constructor(private youtube: YoutubeService) {
}
ngOnInit() {
this.youtube.getVideos().subscribe(data => console.log(data));
}
}
// Service
export class YoutubeService {
videoSubscription: Observable<any>;
API_KEY: string = 'someKey';
CHANNEL_ID: string = 'someID';
configUrl: string = `https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=${this.CHANNEL_ID}&maxResults=50&order=date&type=video&key=${this.API_KEY}`
constructor(private http: HttpClient) {
this.videoSubscription = this.http.get(this.configUrl); // create observable for external subscriptions
}
getVideos() {
return this.videoSubscription;
}
}
要填充此对象(如果您正在使用asp.net/core),则应阅读有关ModelBinders https://docs.microsoft.com/pl-pl/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.2的信息。
答案 1 :(得分:0)
也许您可以利用Newtonsoft.JSonConvert吗?
我的结构有点简化(例如,用字符串代替日期),但是如果您检查生成的json,它就可以满足您的需求。
public class Class1
{
static void Main()
{
meetingCentre myCenter = new meetingCentre("EBC-MC_C7");
meetingRoom myRoom = new meetingRoom("EBC-C7-MR:1_1");
myCenter.rooms.Add(myRoom);
myRoom.reservations.Add(new reservation("28.10.2016", "10:00", "11:30", 4, "College", false, ""));
myRoom.reservations.Add(new reservation("28.10.2016", "12:00", "13:30", 4, "College", false, ""));
myRoom.reservations.Add(new reservation("29.10.2016", "10:00", "11:30", 4, "College", false, ""));
Console.WriteLine(JsonConvert.SerializeObject(myCenter, Formatting.Indented));
Console.ReadLine();
}
}
public class meetingCentre
{
public List<meetingRoom> rooms = new List<meetingRoom>();
public string id;
public meetingCentre(string id)
{
this.id = id;
}
}
public class meetingRoom
{
public List<reservation> reservations = new List<reservation>();
public string id;
public meetingRoom(string id)
{
this.id = id;
}
}
public class reservation
{
public reservation(string d, string f, string t, int pc, string cust, bool v, string n)
{
date = d;
from = f;
to = t;
expectedPersonsCount = pc;
customer = cust;
videoConference = v;
note = n;
}
public string date { get; set; }
public string from { get; set; }
public string to { get; set; }
public int expectedPersonsCount { get; set; }
public string customer { get; set; }
public bool videoConference { get; set; }
public string note { get; set; }
}
}