将JSON反序列化为对象C#
你好,我急需帮助。我有一个json文件,其中包含一个json对象数组。我不知道如何将其反序列化到该对象的列表中 我的JSON在文件中是这种格式的-它有数千行长这只是一个示例:将JSON反序列化为对象C#,c#,json,C#,Json,你好,我急需帮助。我有一个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, "Dou
[{
"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个这样的对象。我试图反序列化该对象,但没有成功。在这一点上,我准备从头开始,我只是在寻找一些方向,将这些数据转换成一个可用的对象
谢谢。Newtonsoft提供了一种方法
CustomClass myClassWithCollection = JsonConvert.DeserializeObject<CustomClass>(jsonString);
CustomClass myClassWithCollection=JsonConvert.DeserializeObject(jsonString);
使用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
公共类帐户
{
公共字符串电子邮件{get;set;}
公共bool活动{get;set;}
公共日期时间CreatedDate{get;set;}
公共IList角色{get;set;}
}
//将JSON字符串反序列化为类型化对象的代码
字符串json=@”{
“电子邮件”:james@example.com',
“活动”:正确,
“CreatedDate”:“2013-01-20T00:00:00Z”,
“角色”:[
“用户”,
“管理员”
]
";
Account=JsonConvert.DeserializeObject(json);
Console.WriteLine(account.Email);
// james@example.com
使用(StreamReader=newstreamreader(@“路径”))
{
字符串jsonString=reader.ReadToEnd();
JArray myObj=(JArray)JsonConvert.DeserializeObject(jsonString);
var player=新玩家();
player.statsBase=myObj.ToObject();
}
这就是我最终完成它的方式。我不确定上述答案是否适用于同一文件中的多个JSON对象。您可以使用Visual Studio 2013、2015从JSON创建模型类,我做到了,并且我很好地解析了JSON。 要使用此功能,剪贴板中必须有JSON/XML,将光标放在.cs文件中,然后使用选项Edit>Paste Special>Paste JSON AS class 查看生成的代码:
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
现在,您可以使用静态类jsconvert中的gerenric方法反序列化它,如下所示:
Rootobject object = JsonConvert.DeserializeObject<Rootobject>(jsonString);
Rootobject object=JsonConvert.DeserializeObject(jsonString);
尝试使用Newtonsoft.Json
库
将此添加到您的项目中
这是我的解决方案的链接:
然后:
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));
}
}
列表对象=
Newtonsoft.Json.JsonConvert。
反序列化对象(jsonString);
foreach(obj中的词典lst)
{
Console.WriteLine(“--NewObject-->”);
Console.WriteLine(string.Format(“Rk:{0}Gcar:{1}”、lst[“Rk”]、lst[“Gcar”]);
foreach(lst中的KeyValuePair项)
{
WriteLine(string.Format(“Key:{0}值:{1}”,item.Key,item.Value));
}
}
很高兴为您提供帮助!您是否遇到了错误,或者具体出了什么问题?请将您的代码与您遇到的错误一起发布。它说,每当我尝试将其放入JObject时,它都无法将其识别为json。因此,我继续使用json对象中的所有字段创建一个类,我想知道获取该类的最佳方法正在将数据放入此类型的列表中?它是JsonArray?您正在使用哪个库?string jsonString=reader.ReadToEnd();JArray myObj=(JArray)JsonConvert.DeserializeObject(jsonString);试试我的答案,这是一个更好的方法。很好的提示!有很多好的答案,但这里的生活正在改变!很好的提示。你做了我的一天很好!!感谢你用新方法创建json到c#object。真是太棒了。幸运的是我读到了这篇文章,我通常总是手动创建它们----
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));
}
}