Skip to main content

There is an alternate way to write these complex JSON using Expando object or XElement and then serialize.

https://blogs.msdn.microsoft.com/csharpfaq/2009/09/30/dynamic-in-c-4-0-introducing-the-expandoobject/

dynamic contact = new ExpandoObject();
{    
contact.    Name = “Patrick"Patrick Hines”;
Hines",
contact.    Phone = “206"206-555-0144”;
0144",
contact.    Address = new ExpandoObject();
    {    
contact.Address.        Street = “123"123 Main St”;
St",
contact.Address.        City = “Mercer"Mercer Island”;
Island",
contact.Address.        State = “WA”;
"WA",    
contact.Address.        Postal = “68402”;"68402"
    }
};

//Serialize to get Json string using NewtonSoft.JSON
string Json = JsonConvert.SerializeObject(contact);

There is an alternate way to write these complex JSON using Expando object or XElement and then serialize.

https://blogs.msdn.microsoft.com/csharpfaq/2009/09/30/dynamic-in-c-4-0-introducing-the-expandoobject/

dynamic contact = new ExpandoObject();

contact.Name = “Patrick Hines”;

contact.Phone = “206-555-0144”;

contact.Address = new ExpandoObject();

contact.Address.Street = “123 Main St”;

contact.Address.City = “Mercer Island”;

contact.Address.State = “WA”;

contact.Address.Postal = “68402”;

//Serialize to get Json string using NewtonSoft.JSON
string Json = JsonConvert.SerializeObject(contact);

There is an alternate way to write these complex JSON using Expando object or XElement and then serialize.

https://blogs.msdn.microsoft.com/csharpfaq/2009/09/30/dynamic-in-c-4-0-introducing-the-expandoobject/

dynamic contact = new ExpandoObject
{    
    Name = "Patrick Hines",
    Phone = "206-555-0144",
    Address = new ExpandoObject
    {    
        Street = "123 Main St",
        City = "Mercer Island",
        State = "WA",    
        Postal = "68402"
    }
};

//Serialize to get Json string using NewtonSoft.JSON
string Json = JsonConvert.SerializeObject(contact);
Source Link
sudhansu63
  • 6.2k
  • 4
  • 41
  • 55

There is an alternate way to write these complex JSON using Expando object or XElement and then serialize.

https://blogs.msdn.microsoft.com/csharpfaq/2009/09/30/dynamic-in-c-4-0-introducing-the-expandoobject/

dynamic contact = new ExpandoObject();

contact.Name = “Patrick Hines”;

contact.Phone = “206-555-0144”;

contact.Address = new ExpandoObject();

contact.Address.Street = “123 Main St”;

contact.Address.City = “Mercer Island”;

contact.Address.State = “WA”;

contact.Address.Postal = “68402”;

//Serialize to get Json string using NewtonSoft.JSON
string Json = JsonConvert.SerializeObject(contact);