I have the following code which must serialize c# classes into Json objects . The problem is i cannot get the other classes to be serialized and one go . How to i assign the variable data to all other classes so i can serialize them into one json file .
static void Main(string[] args)
{
Rootobject data = new Rootobject();
var dataString = JsonConvert.SerializeObject(data);
File.WriteAllText("Output.json", dataString);
}
public class Rootobject
{
public string Number { get; set; }
public string RequestedDeviceType { get; set; }
public string DeliveryMethod { get; set; }
public Customer Customer { get; set; }
public Vehicle Vehicle { get; set; }
}
public class Customer
{
public Contacts Contacts { get; set; }
public string Name { get; set; }
public string Number { get; set; }
public bool OverrideData { get; set; }
}
public class Contacts
{
public string FirstName { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string City { get; set; }
public string Address { get; set; }
public string MobilePhone { get; set; }
}
public class Vehicle
{
public string VIN { get; set; }
public string MakeModelCode { get; set; }
public string LicensePlate { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public int YearOfInitialRegistration { get; set; }
public string MotorType { get; set; }
public bool OverrideData { get; set; }
}
My Json output after serialization should look as follows
{
"Number": "DTY28968YU",
"RequestedDeviceType": "MHubObd",
"DeliveryMethod": "ByHand",
"Customer": {
"Contacts": {
"FirstName": "Mike",
"Name": "Paul",
"Email": "mail@demo.com",
"City": "San Luis",
"Address": "No 10 Highway Road , San Luis",
"MobilePhone": "+54000000000"
},
"Name": "John Doe",
"Number": "PolicyNumber24",
"OverrideData": true
},
"Vehicle": {
"VIN": "VIN004001",
"MakeModelCode": "34010",
"LicensePlate": "SS 100 GP",
"Make": "RANGE ROVER",
"Model": "SPORT",
"YearOfInitialRegistration": 2016,
"MotorType": "Petrol",
"OverrideData": true
}
}