JSON序列化对象成C# [英] Deserialize JSON into Object C#
问题描述
您好,我的一些急需帮助。我有一个JSON文件,使用JSON对象的数组。我无法弄清楚如何将其反序列化到这个对象的列表。
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.
我的JSON是这种格式的文件 - 这是千排长队的,这只是一个例子:
My JSON is in this format in the file - it is thousands of lines long 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"
}
]
有该文件中这些对象142。我试图对象反序列化无济于事。在这一点上我已经准备好要从头开始,我只是在寻找一些方向得到这个数据转换成有用的对象呢?
There is 142 of these objects in the file. I have attempted to Deserialize the object to no avail. At this point I'm ready to start from scratch and I'm just looking for some direction to get this data into a usable object?
感谢您。
推荐答案
您可以使用Visual Studio 2013年,2015年从JSON创建模型类,我做到了,我分析的JSON罚款。
。要使用此功能,您必须JSON / XML在剪贴板,将光标置于一个cs文件里,然后使用选项修改>粘贴>粘贴JSON作为类
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
看生成的代码:
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用下面的命令来安装这样的:
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
现在,你可以反序列化它,使用gerenric方法 DeserializedObject 从静态类的 JsconCovert ,这样的:
Now you can deserialized it, using the gerenric method DeserializedObject from the static class JsconCovert, like that:
Rootobject object = JsonConvert.DeserializeObject<Rootobject>(jsonString);
这篇关于JSON序列化对象成C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!