Destroy JSON in Object C #

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

My JSON is in this format in a file - it's thousands of lines, this is just a sample:

[{
"Rk": 1,
"Gcar": 467,
"Gtm": 1,
"Date": "Apr 6",
"Tm": "CLE",
"Where": "@",
"Opp": "HOU",
"Rslt": "L0-2",
"Inngs": "CG",
"PA": 4,
"AB": 4,
"R": 0,
"H": 0,
"Doubles": 0,
"Triples": 0,
"HR": 0,
"RBI": 0,
"BB": 0,
"IBB": 0,
"SO": 0,
"HBP": 0,
"SH": 0,
"SF": 0,
"ROE": 0,
"GDP": 0,
"SB": 0,
"CS": 0,
"BA": 0,
"OBP": 0,
"SLG": 0,
"OPS": 0,
"BOP": 2,
"aLI": 0.93,
"WPA": -0.093,
"RE24": -0.64,
"DFSDK": 0,
"DFSFD": -1,
"Pos": "Doubles"
},

{
"Rk": 2,
"Gcar": 468,
"Gtm": 2,
"Date": "Apr 8",
"Tm": "CLE",
"Where": "@",
"Opp": "HOU",
"Rslt": "W2-0",
"Inngs": "CG",
"PA": 4,
"AB": 4,
"R": 0,
"H": 2,
"Doubles": 0,
"Triples": 0,
"HR": 0,
"RBI": 0,
"BB": 0,
"IBB": 0,
"SO": 0,
"HBP": 0,
"SH": 0,
"SF": 0,
"ROE": 0,
"GDP": 0,
"SB": 0,
"CS": 0,
"BA": 0.25,
"OBP": 0.25,
"SLG": 0.25,
"OPS": 0.5,
"BOP": 3,
"aLI": 0.71,
"WPA": -0.008,
"RE24": -0.2,
"DFSDK": 6,
"DFSFD": 1.5,
"Pos": "Doubles"
}
]

The file contains 142 of these objects. I tried to deserialize the object to no avail. At this point, am I ready to start from scratch, and am I just looking for some direction so that this data can be used in a useful object?

Thanks.

+3
source share
5 answers

Visual Studio 2013, 2015, json, , JSON. , JSON/XML , .cs, a > > JSON AS

Insert special JSON as classes

, :

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

JSON , Visual Studio, Newtonsoft.Json, nuget :

Install-Package Newtonsoft.Json

, gerenric DeserializedObject JsconCovert, :

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

Newtonsoft.JSON, , .

:

public class Account
{
    public string Email { get; set; }
    public bool Active { get; set; }
    public DateTime CreatedDate { get; set; }
    public IList<string> Roles { get; set; }
}

// code to deserialize from JSON string to a typed object
string json = @"{
    'Email': 'james@example.com',
    'Active': true,
    'CreatedDate': '2013-01-20T00:00:00Z',
    'Roles': [
        'User',
        'Admin'
    ]
";

Account account = JsonConvert.DeserializeObject<Account>(json);

Console.WriteLine(account.Email);
// james@example.com
+1

Newtonsoft .

CustomClass myClassWithCollection = JsonConvert.DeserializeObject<CustomClass>(jsonString);
0
using(StreamReader reader = new StreamReader(@"path"))
{
     string jsonString = reader.ReadToEnd();
     JArray myObj = (JArray)JsonConvert.DeserializeObject(jsonString);

     var player = new Player();
     player.statsBase = myObj.ToObject<List<StatsBase>>();
}

, , . , JSON .

0

Newtonsoft.Json.

https://www.nuget.org/packages/Newtonsoft.Json/7.0.1 .

: https://dotnetfiddle.net/eqJXTy

:

List<Dictionary<string, string>> obj =
    Newtonsoft.Json.JsonConvert.
        DeserializeObject<List<Dictionary<string, string>>>(jsonString);

foreach(Dictionary<string, string> lst in obj)
{
    Console.WriteLine("--NewObject--");
    Console.WriteLine(string.Format("Rk: {0} Gcar: {1}", lst["Rk"], lst["Gcar"]));
    foreach(KeyValuePair<string, string> item in lst)
    {
        Console.WriteLine(string.Format("Key: {0} Value: {1}", item.Key, item.Value));
    }
}

!

0

Source: https://habr.com/ru/post/1622457/


All Articles