Deserialize JSON into Object C#

前端 未结 5 1627
广开言路
广开言路 2020-11-28 14:06

Hello I am in desperate need of some help. I have a json file, with an array of json objects. I cannot figure out how to deserialize it into a list of this object.

5条回答
  •  萌比男神i
    2020-11-28 14:20

    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

    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(jsonString); 
    

提交回复
热议问题