Home > .net 3.5, AJAX, c#, JavaScript, Visual Studio 2008 > To and FromJson Extension Methods

To and FromJson Extension Methods

October 3, 2007

In his recent blog post, Scott Guthrie provided us with an excellent example of converting objects To JSON using Extension Methods. One thing that was missing, however, was how to convert FROM Json back to Objects and deserialize them.  Thankfully, that requires the same number of codes and a bit of work with generics.

Here’s Scott’s original code for reference (I changed the casing a bit…):

public static class Json


public static string ToJson (this object obj)


       JavaScriptSerializer serializer = new JavaScriptSerializer();

       return serializer.Serialize(obj);



public static string ToJson (this object obj, int recursionDepth)


       JavaScriptSerializer serializer = new JavaScriptSerializer();

serializer.RecursionLimit = recursionDepth;

       return serializer.Serialize(obj);



To that, I added one more method (skipping the recusion depth example for brevity):

public static T FromJson<T> (this object obj)


JavaScriptSerializer serializer = new JavaScriptSerializer();

       return serializer.Deserialize<T>(obj as string);


With this code, the consumer can pass a Type of object along with the string/stream/etc. and rebuild the objects.

Here’s how to use it.  In this example, we’ll use an object from our libraries that goes out and populates “School” objects with their demographic information.

List<School> sch = School.GetSchool();

string jsonString = sch.ToJson();

string jsonSnippet = jsonString.Substring(0, 200);  // Trim the results


Our School objects are contained in a generic List object and then transferred to the Json extension.  The result, a string that looks similar to the one below.  It’s trimmed a bit for the information, but it outputs one per school and is ready for consumption by other sources.


Now, to look at converting it back to objects.  You COULD convert it to string or something else, but, with how I plan to use it, this would be a situation that I know what is coming down the wire and I’m rebuilding it.

List<School> schoolFromJson = jsonString.FromJson<List<School>>();

foreach (School school in schoolFromJson)




As you can see from the watch below, it recreated (quite quickly) all 102 School objects and repopulated the list.

JSON to Object Results


Tags: , , , , , ,

  1. October 3, 2007 at 5:43 pm

    I can’t WAIT to get back into 2008. So are you done done with school or done for now until you decide to go back to school? 😉

  2. October 3, 2007 at 5:52 pm

    I are ❤ 2008… I’ve totally switched my environment to it and use it’s multi-homing to work on 2.0 and “official” projects… everything else is being tested and dev’ed in 3.5 under the pre-roll licensing.

    I’ll have my last class November 29th and walk for my Masters mid-December (ehh, one of those weekends.. I should find out, shouldn’t I..). Then yeah, for now, I’ll be done. I’d like to get my doctorates, but the program I want isn’t available around here (organizational leadership focusing on technology integration and empowerment)… I’d have to go up to KU, which isn’t out of the question. 🙂

  3. Craig
    June 12, 2008 at 3:06 pm

    Nice tip, really like it and I’m testing it out now in my project

  4. June 12, 2008 at 3:08 pm


    Be sure to check out the update to this post using the new .net 3.5 JSON classes.


  1. October 10, 2007 at 2:19 pm
  2. October 29, 2007 at 3:37 pm
  3. April 2, 2008 at 9:47 am
Comments are closed.
%d bloggers like this: