Click here to Skip to main content
15,118,573 members
Articles / Programming Languages / Javascript
Posted 13 Jun 2011

Tagged as


2 bookmarked

Deserialize JSON with C#

Rate me:
Please Sign up or sign in to vote.
4.55/5 (8 votes)
14 Jun 2011CPOL
.NET Framework actually has this functionality built-in.The object is this one: system.web.script.serialization.javascriptserializer.aspx[^]I use this a lot and it works as expected.Here's 2 examples:1. Dumb deserializationHere's the easyest way, where you get what you...
.NET Framework actually has this functionality built-in.
The object is this one: system.web.script.serialization.javascriptserializer.aspx[^]

I use this a lot and it works as expected.

Here's 2 examples:

1. Dumb deserialization
Here's the easyest way, where you get what you give.
Note that I deliberatly added one more property to the last object but the deserializer doesn't bother about that. It will return an array of objects. Deal with it as you wish.
var s = new System.Web.Script.Serialization.JavaScriptSerializer();
Object obj = s.DeserializeObject("[{name:\"Alex\", age:\"34\"}, {name:\"Code\", age:\"55\", size:180}]");

2. Typed deserialization
This one is much smarter. It will convert each item into the passed object and return a List<mytype>.
Note that I've kept the extra property on the last object even knowing that myType doesn't have a match property for it.
var s = new System.Web.Script.Serialization.JavaScriptSerializer();
List<myObject> obj = s.Deserialize<List<myObject>>("[{name:\"Alex\", age:\"34\"}, {name:\"Code\", age:\"55\", size:180 }]");

public class myObject
    public string name { get; set; }
    public int age { get; set; }


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


About the Author

Switzerland Switzerland
Senior IT Consultant working in Switzerland as Senior Software Engineer.

Find more at on my blog.

Comments and Discussions

QuestionMake it generic so that can be applicable for any number of JSON Columns. Pin
C-Sharp14-Apr-16 18:29
MemberC-Sharp14-Apr-16 18:29 
AnswerRe: Make it generic so that can be applicable for any number of JSON Columns. Pin
AlexCode5-Apr-16 13:33
professionalAlexCode5-Apr-16 13:33 
GeneralMy vote of 1 Pin
me_pollack11-Dec-14 13:22
Memberme_pollack11-Dec-14 13:22 
GeneralRe: My vote of 1 Pin
AlexCode11-Dec-14 20:44
professionalAlexCode11-Dec-14 20:44 
QuestionDumb Serialization Pin
Pascualito16-Apr-14 11:33
professionalPascualito16-Apr-14 11:33 
AnswerRe: Dumb Serialization Pin
AlexCode19-Apr-14 11:29
professionalAlexCode19-Apr-14 11:29 
GeneralRe: Dumb Serialization Pin
Pascualito26-Apr-14 12:13
professionalPascualito26-Apr-14 12:13 
GeneralRe: Dumb Serialization Pin
AlexCode27-Apr-14 21:45
professionalAlexCode27-Apr-14 21:45 
GeneralRe: Dumb Serialization Pin
Pascualito30-Apr-14 8:27
professionalPascualito30-Apr-14 8:27 
GeneralRe: Yeah, makes sense since the whole system.web namespace seems... Pin
AlexCode20-Jun-11 22:01
professionalAlexCode20-Jun-11 22:01 
GeneralRe: It also doesn't work under Client Profile (Only the full fra... Pin
kamahl20-Jun-11 19:32
Memberkamahl20-Jun-11 19:32 
GeneralRe: When I first submitted this, it didn't work, hence the tip/t... Pin
ricmrodrigues14-Jun-11 22:52
Memberricmrodrigues14-Jun-11 22:52 
GeneralDoesn't work with List<T>, which is a big advantage here. Pin
ricmrodrigues14-Jun-11 1:51
Memberricmrodrigues14-Jun-11 1:51 
GeneralRe: Next time I recommend you read the specs. It does handle col... Pin
AlexCode14-Jun-11 5:39
professionalAlexCode14-Jun-11 5:39 

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.