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.
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.
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
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:
Install-Package Newtonsoft.Json
Now you can deserialized it, using the gerenric method DeserializedObject from the static class JsconCovert, like that:
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.
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
Try using Newtonsoft.Json
library.
Add this https://www.nuget.org/packages/Newtonsoft.Json/7.0.1 on your project.
This is the link for my solution: 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!
Newtonsoft provide a way to do it.
CustomClass myClassWithCollection = JsonConvert.DeserializeObject<CustomClass>(jsonString);