-1

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
   }
}
Marisa
  • 732
  • 6
  • 22
Carlos.Net
  • 97
  • 2
  • 3
  • 9
  • My Json file should look as follows – Carlos.Net Oct 30 '17 at 12:40
  • https://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4 – Stavm Oct 30 '17 at 12:44
  • 1
    If you don't assign data to `data`, the reference types are `null` and you won't see it serialized. Why don't you *try* to put in data like you show and see what happens? – crashmstr Oct 30 '17 at 12:46
  • @Carlos.Net you have not initialized `Vehicle`, `Customer`... to any value. The default null value handling is to omit objects that are `null`. Try to initiaze the values to meaningful data and retry the serialization process – Michael Mairegger Oct 30 '17 at 12:46
  • 1
    Possible duplicate of [Turn C# object into a JSON string in .NET 4](https://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4) – Dour High Arch Oct 30 '17 at 13:00

1 Answers1

0

You just need to instantiate that data. For instance:

var r = new Rootobject();
r.Customer = new Customer { Name = "John Doe", Number="PolicyNumber24", 
    OverrideData=true};
var dataString = JsonConvert.SerializeObject(r);

Instantiate the other classes as well, and your serialization should work.

Palle Due
  • 5,929
  • 4
  • 17
  • 32