Producing Json in correct format

91 Views Asked by At

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?

2

There are 2 best solutions below

4
On BEST ANSWER

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.

public class Site
{
    public string id { get; set; }
    public string Sitename { get; set; }
    public string website { get; set; }
}

public class Product
{
    public string Name { get; set; }
    public string Year { get; set; }
}

public class RootObject
{
    public string retailer { get; set; }
    public List<Site> sites { get; set; }
    public List<Product> Products { get; set; }
}

You should now be able to set the data in RootObject and serialize this correctly.

0
On

Create classes like this

public class Site
{
    public string id { get; set; }
    public string Sitename { get; set; }
    public string website { get; set; }
}

public class Product
{
    public string Name { get; set; }
    public string Year { get; set; }
}

public class Example
{
    public string retailer { get; set; }
    public List<Site> sites { get; set; }
    public List<Product> Products { get; set; }
}

and then put all your data in the objects like this

Example par = new Example();
List<Site> parList = new List<Site>();
Site site = new Site();
Product sw = new Product();
List<Product> swList = new List<Product>();
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.Now.Year.ToString();
swList.Add(sw);
par.Products = swList;
parList.Add(site);  

then just Serialize your object with the help of Newtonsoft

string ser = JsonConvert.SerializeObject(par);
Console.WriteLine(ser);

This is how ser looks

Screenshot of <code>ser</code>