RSS

JSON Array Deserialization in C# With Extension Method

11 May

After my first post about Json Serialization and Deserialization, I quickly realize I also need to deserialize object from Json array. Well, the previous code doesn’t work on deserializing Json array. My Json array looks like this:

[ { "firstname": "Alexander", "lastname": "Smith", "email": "asmith@company.com" }, { "firstname": "George", "lastname": "McAllen", "email": "gmcallen@company.com" } ]

The code for deserializing Json array is:

public static List<T> FromJsonArray(this string s)
{
    javaScriptSerializer = new JavaScriptSerializer();

    javaScriptSerializer.Deserialize<List<T>>(s);
}

It pretty much works the same way, only difference is instead of using just T, I use List<T> as return type. Everything else is still the same.

Advertisements
 
3 Comments

Posted by on May 11, 2011 in General

 

Tags: , , ,

3 responses to “JSON Array Deserialization in C# With Extension Method

  1. manasa

    July 19, 2011 at 4:03 am

    How to retrieve the Data from the JSON Object by filtrating the data with different condition?Can you explain with an example?… in c# 4.0.

    Pls Reply ASAP

    Thanks in advance…

     
  2. pbb72

    March 8, 2012 at 3:04 am

    How can you loop through the list elements? I keep on getting “Object reference not set to an instance of an object”, even when just accessing the .Count property of the list…

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: