can you please help me how to create Json file in C# like in this example below? I have list of centres with list of rooms inside and inside of rooms is list of reservations (date of reservation is one of the elements of reservation). I don't know how to create from my list this data structure.
"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": ""
}
]
}
},
....
Actually I think it depends what what is the use case. If you want to use it as DTO you should consider using a dictionary for reservation collection.
public class Meeting
{
public string MeetingCentre { get; set; }
public string MeetingRoom { get; set; }
public Dictionary<string, Reservation> ReservationSchedule { get; set; }
}
public class Reservation
{
public string From { get; set; }
//Other properties
}
To populate this object(if you are working with asp.net/core) you should read about model ModelBinders https://docs.microsoft.com/pl-pl/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.2 .
maybe you can take advantage of Newtonsoft.JSonConvert ?
My struct is a bit simplified (ex. strings instead of dates) but if you check the resulting json, it could meet your needs.
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; }
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.