gpt4 book ai didi

c# - 将 JSON 反序列化为对象 C#

转载 作者:太空狗 更新时间:2023-10-29 22:06:10 24 4
gpt4 key购买 nike

您好,我急需帮助。我有一个 json 文件,其中包含一组 json 对象。我不知道如何将它反序列化到这个对象的列表中。

我的 JSON 在文件中采用这种格式 - 它有数千行,这只是一个示例:

[{
"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"
}
]

文件中有 142 个这样的对象。我试图反序列化对象无济于事。此时我已准备好从头开始,我只是在寻找将这些数据转化为可用对象的方向?

谢谢。

最佳答案

您可以使用 Visual Studio 2013、2015 从 json 创建您的模型类,我做到了,并且我很好地解析了 JSON。要使用此功能,您的剪贴板中必须有 JSON/XML,将光标放在 .cs 文件中,然后使用选项Edit > Paste Special > Paste JSON AS Classes

Paste 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

现在您可以反序列化它,使用静态类 JsconCovert 中的 gerenric 方法 DeserializedObject,如下所示:

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

关于c# - 将 JSON 反序列化为对象 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34302845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com