Click here to Skip to main content
Click here to Skip to main content

Deserialize JSON with C#

By , 14 Jun 2011
 
Introduction

The motive for this howto is, you have a JSON string, and you want to convert it, not to an C# Object, but to a List<> of that same type, and the .NET Framework doesn't give you the tools to do it out-of-the-box. Here you can see how to do it.
 
Using the code

You're using jQuery or any other way of making XHRs, making some fancy ajax requests, but tired of Request["setting"] to get data from client-side, starving for a strong-typed approach? STOP! I've got just what you need, and supports single object, and even lists of objects (arrays)!
 
Example:
Single: { "field1":"value1","field2":"value2" }
Array: [ { "field1":"value1","field2":"value2" }, { "field1":"value1","field2":"value2" } ]
 
For the single, the .NET Framework has the key, you need to create a class that has public fields or properties matching the field names on your JSON string, example:
public class Test
{
  public string field1 { get; set; }
  public string field2 { get; set; } 
}  
And the code to serialize your Single JSON is this:
Test myDeserializedObj = (Test)JavaScriptConvert.DeserializeObject(Request["jsonString"], typeof(Test));
and voilá! You got yourself a strong-typed object with client-side data, sweet!
 
But and if I want a list of data of the same type, say an Array, how is it done? The .NET Framework hasn't got the answer for this one..
You have to download and reference the following DLL
Once it's done:
List<test> myDeserializedObjList = (List<test>)Newtonsoft.Json.JsonConvert.DeserializeObject(Request["jsonString"], typeof(List<test>));

and yes! you're done :)
How easy is this? It's perfect for your lightweight ajax applications with .NET backend.
 
Points of Interest

Simple and effective way of mixing JSON with .NET

License

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

About the Author

ricrodrigues
Software Developer (Senior) Truphone
Portugal Portugal
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberpatriot.pradeep@gmail.com4 Feb '13 - 23:45 
Questionhow to deserialize a simple json to an objectmemberMember 964183830 Nov '12 - 3:09 
AnswerRe: how to deserialize a simple json to an objectmemberricrodrigues30 Nov '12 - 3:16 
GeneralExcellent!memberSunasara Imdadhusen12 Jul '12 - 23:37 
QuestionDll downloadmemberMember 854560820 Mar '12 - 23:05 
AnswerRe: Dll downloadmemberricrodrigues20 Mar '12 - 23:09 
AnswerRe: Dll download [modified]memberMember 854560821 Mar '12 - 21:58 
GeneralRe: Dll downloadmemberricrodrigues21 Mar '12 - 22:21 
Is the JSON you're trying to deserialize valid? Have you validated it against something like http://jsonlint.com/[^]
GeneralReason for my vote of 1 Reinventing the wheel, as this funct...memberRoman_wolf14 Jun '11 - 19:41 
GeneralRe: Why is Newtonsoft.JSON so used then if it's "reinventing the...memberricrodrigues14 Jun '11 - 21:53 
General[edit] escaped &lt; &gt; in code, updated description.subeditorIndivara13 Jun '11 - 21:46 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 14 Jun 2011
Article Copyright 2010 by ricrodrigues
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid