advertise with us

C# – Object to JSON Examples

In this article you can find how to convert C# object into JSON using Json.NET (Newtonsoft.Json) library. It is available as a NuGet package for free, so you can easily install it from nuget.org repository.

Let’s create an example class which object will be converted into JSON.

Now we need to create an instance of Place object.

Having the object we are ready to convert it to JSON string. We’ll use SerializeObject function from JsonConvert class.

The type of json variable is string, so let’s print it to console.

Result:

As we can see the JSON is not formatted and difficult to read. We can solve that issue by calling SerializeObject function passing two parameters where first is object to be serialized and second is formatting option. Choosing Indented we will receive nicely formatted JSON.

Print to console.

Result:


Need Help with Your C# Projects?

We offer expert support and development services for projects of any size. Contact us for a free consultation and see how we can help you succeed.

CONTACT US NOW