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

Deserialize JSON with C#

Rate me:
Please Sign up or sign in to vote.
4.81/5 (28 votes)
14 Jun 2011CPOL1 min read 604.2K   41   21
Convert a JSON string to a List of C# Objects
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)


Written By
Software Developer (Senior) Truphone
Portugal Portugal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks so much - just what I needed Pin
BillCollisNZ4-Mar-17 16:30
BillCollisNZ4-Mar-17 16:30 
Smile | :) Smile | :)

QuestionIssue Pin
EasyHero27-Oct-16 5:24
professionalEasyHero27-Oct-16 5:24 
QuestionVery helpful Pin
Ferd Really22-Jun-15 9:15
Ferd Really22-Jun-15 9:15 
QuestionProblem Pin
Srinivasarao Nalam29-May-15 13:18
Srinivasarao Nalam29-May-15 13:18 
QuestionCalling the List to Source Code using AJAx Pin
Member 1131949717-Dec-14 20:09
Member 1131949717-Dec-14 20:09 
GeneralMy vote of 5 Pin
KP Singh Chundawat13-Nov-14 21:59
KP Singh Chundawat13-Nov-14 21:59 
GeneralMy vote of 3 Pin
Member 373887916-Sep-14 3:24
Member 373887916-Sep-14 3:24 
GeneralThank you Pin
Rex Liu8-Jul-14 2:17
Rex Liu8-Jul-14 2:17 
Question[My vote of 1] Very Poor Pin
msdevtech18-Sep-13 12:43
msdevtech18-Sep-13 12:43 
GeneralMy vote of 4 Pin
patriot.pradeep@gmail.com4-Feb-13 23:45
patriot.pradeep@gmail.com4-Feb-13 23:45 
Questionhow to deserialize a simple json to an object Pin
Member 964183830-Nov-12 3:09
Member 964183830-Nov-12 3:09 
AnswerRe: how to deserialize a simple json to an object Pin
ricmrodrigues30-Nov-12 3:16
ricmrodrigues30-Nov-12 3:16 
GeneralExcellent! Pin
Sunasara Imdadhusen12-Jul-12 23:37
professionalSunasara Imdadhusen12-Jul-12 23:37 
QuestionDll download Pin
Member 854560820-Mar-12 23:05
Member 854560820-Mar-12 23:05 
AnswerRe: Dll download Pin
ricmrodrigues20-Mar-12 23:09
ricmrodrigues20-Mar-12 23:09 
AnswerRe: Dll download Pin
Member 854560821-Mar-12 21:58
Member 854560821-Mar-12 21:58 
GeneralRe: Dll download Pin
ricmrodrigues21-Mar-12 22:21
ricmrodrigues21-Mar-12 22:21 
GeneralRe: Dll download Pin
fertansa2-Feb-16 11:16
fertansa2-Feb-16 11:16 
GeneralReason for my vote of 1 Reinventing the wheel, as this funct... PinPopular
Roman_wolf14-Jun-11 19:41
Roman_wolf14-Jun-11 19:41 
GeneralRe: Why is Newtonsoft.JSON so used then if it's "reinventing the... Pin
ricmrodrigues14-Jun-11 21:53
ricmrodrigues14-Jun-11 21:53 
General[edit] escaped &lt; &gt; in code, updated description. Pin
Indivara13-Jun-11 21:46
professionalIndivara13-Jun-11 21:46 

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.