• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
info@dotnettec.com | +91-8800268750

DotNetTec

  • Home
  • Tutorials
    • Programming Language
      • C#
      • JavaScript
    • .Net Development
      • .NET Framework
      • ASP.NET Web Forms
      • MVC Architecture
      • Web API
      • .NET Core Framework
    • Front End Frameworks
      • JavaScript
      • HTML
    • Cloud Computing
      • Microsoft Azure
    • Database
      • SQL Server
    • Google Maps API
    • RPA
      • Blue Prism
      • UiPath
  • How to
  • Knowledge Base
  • Shop

C# Serialize Object to JSON Newtonsoft

April 21, 2020 by Ravi Kumar Leave a Comment

In this blog post, you will learn how to serialize and deserialize JSON objects using the Newtonsoft library with an example in C#. Keep reading on How to Convert JSON to Datatable in C#

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write and easy for machines to parse and generate. JSON is a text format that is completely language independent.

JSON supports the following two data structures,

  • Collection of name/value pairs and,
  • An ordered list of values.

How to Serialize and Deserialize objects using NewtonSoft JSON

Let’s create a console-based application to understand how to serialize an object to JSON NewtonSoft and NewtonSoft JSON Deserialize using C#.

c# serialize object to json newtonsoft,

Now you need to install the Newtonsoft JSON using Nuget Package in the application. You can visit to find the latest package  https://www.nuget.org/packages/newtonsoft.json/.

Run the below command to install Newtonsoft JSON from the package mange console as shown below:-

Install-Package Newtonsoft.Json -Version 12.0.3

install newtonsoft json

Now find the below example to implement Newtonsoft JSON serialization and deserialization:-

Program.cs

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;

namespace ConsoleAppNewtonsoft
{
    class Program
    {
        const string filePath = @"c:\data\json.txt";
        static void Main(string[] args)
        {
            var product = new Product
            {
                Name = "eBooks",
                SerialNumbers = new List<int> { 101, 102, 103, 104 }
            };

            Console.WriteLine("Object before serialization:");
            Console.WriteLine("----------------------------");
            Console.WriteLine();

            product.Show();

            Serialize(product);

            var deserialized = Deserialize(filePath);

            Console.WriteLine("Deserialized (json) string:");
            Console.WriteLine("---------------------------");
            Console.WriteLine();
            Console.WriteLine(deserialized);
        }

        public static void Serialize(object obj)
        {
            var serializer = new JsonSerializer();

            using (var sw = new StreamWriter(filePath))
            using (JsonWriter writer = new JsonTextWriter(sw))
            {
                serializer.Serialize(writer, obj);
            }
        }

        public static object Deserialize(string path)
        {
            var serializer = new JsonSerializer();

            using (var sw = new StreamReader(path))
            using (var reader = new JsonTextReader(sw))
            {
                return serializer.Deserialize(reader);
            }
        }
    }
}

Product.cs

using System;
using System.Collections.Generic;

namespace ConsoleAppNewtonsoft
{
    public class Product
    {
        public string Name { get; set; }
        public List<int> SerialNumbers { get; set; }
        public void Show()
        {
            Console.WriteLine("Name: " + Name);
            Console.WriteLine("Serial Numbers: " + string.Join<int>(",", SerialNumbers));
            Console.WriteLine();
            Console.WriteLine();
        }
    }
}

Download Source Code

Output Demo
Output

Conclusion

I hope you liked this article on NewtonSoft JSON Serialize C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Category iconC# Tag iconc# serialize object to json newtonsoft,  newtonsoft json deserialize,  newtonsoft json serialize

Subscribe to our blog.

Get the latest posts delivered right to your inbox.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Google Maps Draw Polygon Get Coordinates
  • Check if point inside polygon Google Maps JavaScript
  • How to Create a Calendar Control in ASP.NET
  • Interview Etiquette Before, During and After
  • How to Attend Telephonic Interview Tips

Products

  • Online Shopping ASP.NET Project Source Code Online Shopping ASP.NET Project Source Code
    Rated 5.00 out of 5
    $69.00 $39.00
  • responsive-aspnet-mvc-grid-view ASP.NET MVC full Ajaxify and Bootstrap Grid with CRUD Operation
    Rated 4.67 out of 5
    $21.00 $9.00
  • eCommerce Website Source Code in ASP.NET eCommerce Website Source Code in ASP.NET $99.00
  • Hospital Management System Project ASP.Net Source Code
    Rated 5.00 out of 5
    $99.00
  • Whatsapp Bulk Message Sender Source Code WhatsApp Bulk Message Sender Source Code
    Rated 5.00 out of 5
    $69.00

Footer

.NET Developers Blog

Learn .NET, Programming Languages, Web App Development, APIs Development & Integration, Google Maps JavaScript APIs, and Cloud Services, through the latest articles, step-by-step tutorials, code examples, live projects, interview ebooks, and best practices.

Sitemap

  • About
  • Blog
  • Shop
  • Contact
  • Privacy Policy

Expertise

  • Web Development
  • Custom Software Development
  • Web API Development
  • Google Maps APIs

Newsletter

Get the latest news, posts, and announcements straight to your inbox.

Copyright © 2021 DotNetTec.