0

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?

dbc
  • 104,963
  • 20
  • 228
  • 340
Computer
  • 2,149
  • 7
  • 34
  • 71

2 Answers2

2

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.

Monolithcode
  • 598
  • 8
  • 19
  • This is what i already have. Im not too sure what your suggesting i should change as i already have an instance/List of these classes but the Json is incorrect? – Computer Dec 23 '16 at 09:47
  • From what I can see your classes must be wrong, your setting site.Products = swList; From the json, site has no products, the root object itself - so parent in your case, is what is supposed to hold the products directly. – Monolithcode Dec 23 '16 at 09:50
  • You was on the right lines with this. The documentation i read stated RootObject: retailer - company name, sites - array containing your site name, but had no mention of products. How did you determine i needed to put the properties List here? Im new to Json but any pointers would be great. Will mark this as answer once i get your reply :-) – Computer Dec 23 '16 at 11:15
  • 1
    Good stuff, I once had major trouble mapping json in the past and when i came back to it again a year later I had a moment of clarity, ignore the documentation and manual messing around, if there is an example as you have provided you can just plop it in any site like http://json2csharp.com/ for example, this spits out the json in c# class form so you can see exactly what it is your dealing with, no mess, no fuss. – Monolithcode Dec 23 '16 at 11:24
1

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>

Community
  • 1
  • 1
Mohit S
  • 13,723
  • 6
  • 34
  • 69