1

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); 
     }

 }
}
1
  • 1
    BTW - JavaScriptSerializer is deprecated, you should be using DataContractJsonSerialzer. Commented Oct 28, 2010 at 22:58

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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).

Comments

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.