Click here to Skip to main content
15,888,461 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Deserializing JSON to Object Without Creating Custom Class

Rate me:
Please Sign up or sign in to vote.
4.06/5 (16 votes)
9 Jun 2016CPOL 51K   7   6
Deserializing JSON to object without creating any custom class using C# Dynamic type

Introduction

Usually, we create custom classes to store the deserialized data from json. These custom classes were no more required if we use dynamic type which is introduced in C# 4.0.

Using the Code

Usual way of deserializing json using Newtonsoft:

C#
class Car
{
    public string Name;
    public string Company;
    public int Id;

}
class Program
{
    static void Main(string[] args)
    {
        Car car = new Car() { Id = 1, Name = "Polo", Company = "VW" };
        string serialized = JsonConvert.SerializeObject(car);
        Car deserialized= JsonConvert.DeserializeObject<Car>(serialized);
        Console.WriteLine(deserialized.Company + " " + deserialized.Name);
        Console.ReadLine();
    }
}

New way using dynamic:

C#
class Program
{
    static void Main(string[] args)
    {
        Car car = new Car() { Id = 1, Name = "Polo", Company = "VW" };
        string serialized = JsonConvert.SerializeObject(car);
        dynamic deserialized = JsonConvert.DeserializeObject(serialized);
        Console.WriteLine(deserialized.Company + " " + deserialized.Name);
        Console.ReadLine();
    }
}

As dynamic type skips the compile time binding and performs the binding dynamically, intellisense support will not be available,

Also, if you try to access a non-existant property like deserialized.Price which is not in the json, it will throw neither compile time or run time error. It will be considered as null and no code break.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Microsoft Global Delivery
India India
Yet another software developer who works on various Microsoft and Web technologies but curious to try something beyond an usual

Comments and Discussions

 
QuestionCool, but.... Pin
Michael Breeden20-Jul-16 5:40
Michael Breeden20-Jul-16 5:40 
I wasn't aware of this functionality until reading this article, so that's cool.
I now wanted to use it, because I get unstructured data from a stored procedure and I can request it in a JSON string.
What is uncool is that you did not clarify that it would require NewtonSoft to work apparently, so I was looking for a Microsoft namespace which their terrible documentation suggests exists, but doesn't really seem to such as System.Web.Script.Serialization or System.Runtime.Serialization.JSON.
Actually, you are not alone. A lot of great code examples always seem to be missing namespace information.
Aside from that... cool

QuestionGood for simple payload not for complex Pin
Raj Champaneriya13-Jun-16 3:05
professionalRaj Champaneriya13-Jun-16 3:05 
QuestionPerformance Pin
Dmitriy Gakh10-Jun-16 6:13
professionalDmitriy Gakh10-Jun-16 6:13 
QuestionWhy? Pin
Slacker00710-Jun-16 0:31
professionalSlacker00710-Jun-16 0:31 
AnswerRe: Why? Pin
Dmitriy Gakh10-Jun-16 6:10
professionalDmitriy Gakh10-Jun-16 6:10 
AnswerRe: Why? Pin
Jono Stewart12-Jun-16 20:25
Jono Stewart12-Jun-16 20:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.