Click here to Skip to main content
15,881,831 members
Articles / Programming Languages / Javascript
Alternative
Tip/Trick

Deserialize JSON with C#

Rate me:
Please Sign up or sign in to vote.
4.55/5 (8 votes)
14 Jun 2011CPOL 104.8K   2   14
.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.
C#
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.
XML
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 }]");

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

License

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


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

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.