Cast or deserialize a Json object to a class in C#

less than 1 minute read

Cast or deserialize a Json object to a class in C#

Deserialize Javascript/Json objects using JavaScriptSerializer in C#.

using System.Web.Script.Serialization;

    ////////////////////////////////////////////////
    public class MyClass
    {
        public string name { get; set; }
    }
    ////////////////////////////////////////////////
    
    var data = "{ \"name\":\"Test\" }"; // your json string
    
    JavaScriptSerializer json_serializer = new JavaScriptSerializer();
    try
    {
        MyClass myClassObject = json_serializer.Deserialize<MyClass>(data);
    }
    catch (Exception e)
    {
        throw;
    }

Leave a comment