I'm trying to produce the below Json
{
"retailer": "The retailer",
"sites": [{
"id": "1234",
"Sitename": "Microsoft",
"website": "www.microsoft.com"
}],
"Products": [{
"Name": "Visual Studio",
"Year": "2017"
}]
}
sites (array but I've used a List). Products (array but I've used a List)
My code (which doesnt produce the above Json)
Parent par = new Parent();
List<Sites> parList = new List<Sites>();
Sites site = new Sites();
Software sw = new Software();
List<Software> swList = new List<Software>();
par.retailer = "The retailer";
site.Id = "1234";
site.Sitename = "Microsoft";
site.Website = "www.microsoft.com";
par.sites = parList;
sw.Name = "Visual Studio";
sw.Year = DateTime.Year;
swList.Add(sw);
site.Products = swList;
parList.Add(site);
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Parent));
ser.WriteObject(stream1, par);
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
Console.WriteLine(op);
What i need to do is generate the Json (data coming from a database, but for now i have hard coded the values) and the post it to a third party service but i cant get the Json to be in the format i require? How could i achieve this?
For stuff like this I like sites that convert json to classes, then you know exactly what your working with.
For your example, a little google-fu and I get these classes.
You should now be able to set the data in RootObject and serialize this correctly.