C# Serialize and Deserialize JSON

Created by
Monday, June 6, 2016

In this simple article i demonstrate creating and object, serializing it in JSON and deserializing it back to a new object all using Json.Net Newtonsoft.

First you need to add a reference to Json.NET from your proejct. Open the package manager console and install Json.NET. Installing this NuGet package will add a reference in your solution.

Json.Net makes serializing and deserializing JSON extremely straight forward. Let's begin by create an object. In this example i have written a class for a book which contains a few properties (Name, Genre, Types, Pages).

I then instantiate this and assign it a series of values for each of its properties.

    public class Program
    {
        static void Main(string[] args)
        {
            Book myBook = new Book();
           
            myBook.Name = "C# Programming";
            myBook.Genre = "Technology";
            myBook.Types = new string[] { "Hardback", "Paperback" };
            myBook.Pages = 394;
        }
    }

    public class Book
    {
        public string Name { get; set; }

        public string Genre { get; set; }

        public string[] Types { get; set; }

        public int Pages { get; set; }
    }

 

Now lets Serialize our Book and write the results to the console.

// Serialize Object
            string output = JsonConvert.SerializeObject(myBook);

            Console.WriteLine("Serialize: Our JSON Object: ");
            Console.WriteLine(output);
            Console.WriteLine("");

 

We shall then take that same string “output” and deserialize it back into a new Book.

// Deserialize Object
            Book myNewBook = JsonConvert.DeserializeObject<Book>(output);

 

It couldn't be any simpler than that!

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace JsonObjects
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Serialize and Deserialize JSON Object");
            Console.WriteLine("");

            // Initialise a new Book
            Book myBook = new Book();
            
            // Assign some values to the properties of this book
            myBook.Name = "C# Programming";
            myBook.Genre = "Technology";
            myBook.Types = new string[] { "Hardback", "Paperback" };
            myBook.Pages = 394;


            // Serialize Object
            string output = JsonConvert.SerializeObject(myBook);

            Console.WriteLine("Serialize: Our JSON Object: ");
            Console.WriteLine(output);
            Console.WriteLine("");

            Thread.Sleep(200);

            // Deserialize Object
            Book myNewBook = JsonConvert.DeserializeObject<Book>(output);

            Console.WriteLine("Deserialize: Our Book Object");
            Console.WriteLine(myNewBook.Name);
            Console.WriteLine(myNewBook.Genre);
            Console.WriteLine(myNewBook.Types.FirstOrDefault());
            Console.WriteLine(myNewBook.Pages);

            Console.ReadKey();
        }
    }

    public class Book
    {
        // Add some properties to our book class
        public string Name { get; set; }

        public string Genre { get; set; }

        public string[] Types { get; set; }

        public int Pages { get; set; }
    }
}

All source code available for download:

References:

http://www.newtonsoft.com/json

3
Rating
1 2 3 4 5

Save

Saved

Report

Reported
Reasons
Cancel
0 comments