Deserialize a JSON
Post your questions regarding programming in C# in here.
2 posts
Page 1 of 1
Hi all,
I'm trying to deserialize this json: http://api.football-data.org/alpha/fixtures/133566.
First of all, I've used json2csharp: http://json2csharp.com/ for create the class.
I'm not sure, however, that the generated class is correct in this case.
However I made an Http request:
At this point I save the response into a variable:
The problem is that the foreach will not accept the item, saying that there is no definition of GetEnumerator.
If someone else can deserialize the JSON in a more intelligent and effective, I am happy to read it!
I'm trying to deserialize this json: http://api.football-data.org/alpha/fixtures/133566.
First of all, I've used json2csharp: http://json2csharp.com/ for create the class.
I'm not sure, however, that the generated class is correct in this case.
However I made an Http request:
Code: Select all
With this function, I pass the link of deserialize from JSON, and I will return a string with the response.public string Request(string requestUrl)
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/json";
string responseText;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using (var responseStream = new StreamReader(response.GetResponseStream()))
{
responseText = responseStream.ReadToEnd();
}
return responseText;
}
At this point I save the response into a variable:
Code: Select all
Then create an object containing the list of parameters defined in RootObject previously generated with the tool json2csharp.string responseText = parser.Request(requestUrl);
The problem is that the foreach will not accept the item, saying that there is no definition of GetEnumerator.
Code: Select all
So I want to know if I'm doing something wrong and if the generated class is good or not.var obj = JsonConvert.DeserializeObject<Fixtures.RootObject>(responseText);
foreach (var element in obj) //this insert the value into a Datagrid.
{
MainWindow.AppWindow.Fixtures_Table.Items.Add(new Fixtures.Fixture
{
date = element.date;
etc...
});
}
If someone else can deserialize the JSON in a more intelligent and effective, I am happy to read it!
I'm in the empire business.
Works really well for me and many other commercial applications: http://www.newtonsoft.com/json
2 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023