简体   繁体   English

将JSON反序列化为对象C#

[英]Deserialize JSON into Object C#

Hello I am in desperate need of some help. 你好,我迫切需要一些帮助。 I have a json file, with an array of json objects. 我有一个json文件,带有一个json对象数组。 I cannot figure out how to deserialize it into a list of this object. 我无法弄清楚如何将其反序列化为此对象的列表。

My JSON is in this format in the file - it is thousands of lines long this is just a sample: 我的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"
}
]

There is 142 of these objects in the file. 文件中有142个这样的对象。 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? 在这一点上,我已经准备好从头开始了,我只是想找到一些方向将这些数据转化为可用的对象?

Thank you. 谢谢。

You can use Visual Studio 2013, 2015 to create your model classes from a json, I did it and I parsed the JSON fine. 你可以使用Visual Studio 2013,2015从json创建你的模型类,我做了它并解析了JSON。 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 要使用此功能,您必须在剪贴板中包含JSON / XML,将光标放在.cs文件中,然后使用选项编辑>选择性粘贴>粘贴JSON AS类

将特殊JSON粘贴为类

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: 在运行时将JSON反序列化为从Visual Studio创建的此对象,您可以使用Newtonsoft.Json,您可以使用nuget使用以下命令安装它:

Install-Package Newtonsoft.Json

Now you can deserialized it, using the gerenric method DeserializedObject from the static class JsconCovert , like that: 现在,您可以使用静态类JsconCovert中的gerenric方法DeserializedObject对其进行反序列化 ,如下所示:

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

This is very simple to do using Newtonsoft.JSON and there is a page in the documentation covering how to deserialize an object . 使用Newtonsoft.JSON非常简单,文档中有一个页面,介绍如何反序列化对象

Taken from the documentation page: 摘自文档页面:

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

Newtonsoft提供了一种方法。

CustomClass myClassWithCollection = JsonConvert.DeserializeObject<CustomClass>(jsonString);
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>>();
}

This is how i finally accomplished it. 这就是我最终完成它的方式。 I'm not sure if the above answers will work for multiple JSON objects in the same file. 我不确定上述答案是否适用于同一文件中的多个JSON对象。

Try using Newtonsoft.Json library. 尝试使用Newtonsoft.Json库。

Add this https://www.nuget.org/packages/Newtonsoft.Json/7.0.1 on your project. 在您的项目中添加此https://www.nuget.org/packages/Newtonsoft.Json/7.0.1

This is the link for my solution: https://dotnetfiddle.net/eqJXTy 这是我的解决方案的链接: https//dotnetfiddle.net/eqJXTy

And then: 接着:

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

Happy to help you! 很高兴为您服务!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM