views:

35

answers:

3

From Scottgu's post i got the code to Serialize a collection to JSON object.(i)How to convert the JSON object back to List? (ii) I did not find JavaScriptserializer in System.Runtime.Serialization will the only code work for VS 2010?

ScottGu's snippet

namespace ScottGuHelper.JSON
{
  public static class JSONHelper
  {
      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); 
     }

 }
}
+3  A: 

Reference System.Web.Extensions.dll. It is in the System.Web.Script.Serialization namespace. On the MSDN page for JavaScriptSerializer you'll see where it is located and for which versions of .NET it is available.

Yuriy Faktorovich
+2  A: 

Have a look to the Deserialize method of the same JavascriptSerializer class.

The class is in the System.Web.Script.Serialization namespace

You can find a good example of usage here on SO

Lorenzo
+2  A: 

Refer System.Web.Script.Serialization for JavaScriptSerializer.

Write an extension method to call Deserialize the object.

    public static T JSONtoList<T>(this object jsonObj)
    {
        JavaScriptSerializer _jsserializer = new JavaScriptSerializer();
        return _jsserializer.Deserialize<T>(jsonObj as string);
    }

Hope this helps.(Code is not tested).

Rengaseshan