Search code examples
c#jsonenumerator

Enumerator missing in JSON class


I'm trying to deserialize this json. But I've several problem, in particular the compiler tells me that:

the RootObject not contains a public definition for GetEnumerator.

This is the class generated with json2csharp:

 public class Self
    {
        public string href { get; set; }
    }

    public class Fixtures
    {
        public string href { get; set; }
    }

    public class Players
    {
        public string href { get; set; }
    }

    public class Links
    {
        public Self self { get; set; }
        public Fixtures fixtures { get; set; }
        public Players players { get; set; }
    }

    public class RootObject
    {
        public Links _links { get; set; }
        public string name { get; set; }
        public string code { get; set; }
        public string shortName { get; set; }
        public string squadMarketValue { get; set; }
        public string crestUrl { get; set; }
    }

and this is my code:

var obj = JsonConvert.DeserializeObject<RootObject>(responseText);

            foreach (var item in obj)
            {
                item.name //... this is an example of the attribute that I want grab from the JSON for each team    
            }

I need to iterate over all the teams and save them into a database.

Here is the structure of the JSON, if it can be of any help:

{
"_links":{
"self":{
"href":"http://api.football-data.org/alpha/teams/19"
},
"fixtures":{
"href":"http://api.football-data.org/alpha/teams/19/fixtures"
},
"players":{
"href":"http://api.football-data.org/alpha/teams/19/players"
}
},
"name":"Eintracht Frankfurt",
"code":"SGE",
"shortName":"Eintr. Frankfurt",
"squadMarketValue":"69,050,000 €",
"crestUrl":"http://upload.wikimedia.org/wikipedia/commons/0/04/Eintracht_Frankfurt_Logo.svg"
}

Solution

  • var obj = JsonConvert.DeserializeObject<RootObject>(responseText);
    

    will return one single RootObject and not a collection of objects.

    I think the problem lies in your api call. You are making a request that returns one single team (Eintracht Frankfurt) in this case.

    You want to make a call that returns a list of teams instead.