3

How do i serialize multiple c# classes to one json string using . Be able to make the variables in the json data hold values that can change

string outputJSON = JsonConvert.SerializeObject();

i have the following classes

 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; } } } 

They must serialize to the following structure of json structure and i must be able to capture user input and set the values into json file

{ "Number": "xTest", "RequestedDeviceType": "XXXX", "DeliveryMethod": "XXXX", "Customer": { "Contacts": { "FirstName": "John", "Name": "Doe", "Email": "[email protected]", "City": "Harare", "Address": "XXXXX", "MobilePhone": "00000000" }, "Name": "Peter Chaneta", "Number": "4567865678", "OverrideData": true }, "Vehicle": { "VIN": "weryts55444554", "MakeModelCode": "34010", "LicensePlate": "SS 100 GP", "Make": "RANGE ROVER", "Model": "SPORT", "YearOfInitialRegistration": 2016, "MotorType": "Petrol", "OverrideData": true } } 
1
  • 1
    string output = JsonConvert.SerializeObject(yourobject); does that?!? The json representation reflects your class composition, that's the job Commented Oct 25, 2017 at 22:25

2 Answers 2

6

I am guessing you are using JSON.net. If not you can get it over the Nuget-Packet Manager.

You called your class "Contacts" are you wanting to have more than one contact? If yes, you might want to use a List on the RootObject.

Now if you use:

var data = new Rootobject(); var dataString = JsonConvert.SerializeObject(data); 

You have all your data in a string as requested. From here you could write it to a file. If you want to change the data and read it again you would use something along this:

Rootobject data2 = JsonConvert.DeserializeObject<Rootobject>(dataString); 

Now you could display the data somewhere or could change it through any input controls. (Website / Windows Form / XAML / Console ...)

Any other things you could find at the official documentation for json.net (https://www.newtonsoft.com/json)

Otherwise your question is way too generic.

Sign up to request clarification or add additional context in comments.

Comments

0

As an alternative to JSON.Net you can also use one from System.Web.Extensions namespace:

using System.Web.Extensions; public string SerializeClassToJSON(object baseClass) { var jSerializer = new JavascriptSerializer(); var jsonString = jSerializer.Serialize(baseClass); return jsonString; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.