Deserialize a JSON

Post your questions regarding programming in C# in here.
2 posts Page 1 of 1
Contributors
User avatar
KraZy
Top Poster
Top Poster
Posts: 93
Joined: Sat May 26, 2012 8:40 am

Deserialize a JSON
KraZy
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:
Code: Select all
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;
        }
With this function, I pass the link of deserialize from JSON, and I will return a string with the response.
At this point I save the response into a variable:
Code: Select all
string responseText = parser.Request(requestUrl);
Then create an object containing the list of parameters defined in RootObject previously generated with the tool json2csharp.
The problem is that the foreach will not accept the item, saying that there is no definition of GetEnumerator.
Code: Select all
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...
                });
            }
So I want to know if I'm doing something wrong and if the generated class is good or not.

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.
User avatar
visualtech
VIP - Donator
VIP - Donator
Posts: 265
Joined: Sat Nov 19, 2011 2:19 pm

Re: Deserialize a JSON
visualtech
Works really well for me and many other commercial applications: http://www.newtonsoft.com/json
Image
2 posts Page 1 of 1
Return to “General coding help”