Deserialize JSON into Object C#

You can use Visual Studio 2013, 2015 to create your model classes from a json, I did it and I parsed the JSON fine.
To use this feature, you must have JSON/XML in your clipboard, put your cursor inside a .cs file and then use the option Edit > Paste Special > Paste JSON AS Classes

Paste Special JSON as classes

Look the code that was generated:

public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public int Rk { get; set; }
    public int Gcar { get; set; }
    public int Gtm { get; set; }
    public string Date { get; set; }
    public string Tm { get; set; }
    public string Where { get; set; }
    public string Opp { get; set; }
    public string Rslt { get; set; }
    public string Inngs { get; set; }
    public int PA { get; set; }
    public int AB { get; set; }
    public int R { get; set; }
    public int H { get; set; }
    public int Doubles { get; set; }
    public int Triples { get; set; }
    public int HR { get; set; }
    public int RBI { get; set; }
    public int BB { get; set; }
    public int IBB { get; set; }
    public int SO { get; set; }
    public int HBP { get; set; }
    public int SH { get; set; }
    public int SF { get; set; }
    public int ROE { get; set; }
    public int GDP { get; set; }
    public int SB { get; set; }
    public int CS { get; set; }
    public float BA { get; set; }
    public float OBP { get; set; }
    public float SLG { get; set; }
    public float OPS { get; set; }
    public int BOP { get; set; }
    public float aLI { get; set; }
    public float WPA { get; set; }
    public float RE24 { get; set; }
    public int DFSDK { get; set; }
    public float DFSFD { get; set; }
    public string Pos { get; set; }
}

In runtime to deserialize JSON into this object created from Visual Studio, you can use Newtonsoft.Json, you can install this using nuget with the following command:

Install-Package Newtonsoft.Json

Now you can deserialized it, using the gerenric method DeserializedObject from the static class JsconCovert, like that:

Rootobject object = JsonConvert.DeserializeObject<Rootobject>(jsonString); 

Leave a Comment