Skip to content

Latest commit

 

History

History
27 lines (21 loc) · 772 Bytes

cast-json-object-to-class.md

File metadata and controls

27 lines (21 loc) · 772 Bytes

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