Unable to deserialize soccer json data

I want to create a Windows Forms Application that uses data from football-data.org. This is my first time working with restful / JSON and I am stuck.

When I try to get all the leagues that football-data.org provides, this is the answer you get from the football-data.org api: answer

I am using the following code to get all the correct data: (lbResultBox is a list)

private void btAllLeagues_Click(object sender, EventArgs e)
{
    lbResultBox.Items.Clear();
    List<Leagues> LL = GetLeagues("soccerseasons");
    foreach (var item in LL)
    {
       lbResultBox.Items.Add(item.caption);
    }
}

public static List<Leagues> GetLeagues(string endurl)
{
    var syncClient = new WebClient();
    var content = syncClient.DownloadString(baseurl + endurl);
    return JsonConvert.DeserializeObject<List<Leagues>>(content);
}

public class Leagues
{
    public IDictionary<string,LeagueLinks> _links { get; set; }
    public string caption { get; set; }
    public string league { get; set; }
    public string year { get; set; }
    public string numberOfTeams { get; set; }
    public string numberOfGames { get; set; }
    public string lastUpdated { get; set; }
}

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

      

It works.

But when I try to get all teams from the league, this is the answer I get from the api. I am using this code:

private void btAllTeams_Click(object sender, EventArgs e)
{
    List<LeagueTeams> LT = GetLeagueTeams("soccerseasons/" + id + "/teams");
}

public static List<LeagueTeams> GetLeagueTeams(string endurl)
{
   var syncClient = new WebClient();
   var content = syncClient.DownloadString(baseurl + endurl);
   return JsonConvert.DeserializeObject<List<LeagueTeams>>(content);
}

public class LeagueTeams
{
    public IDictionary<string, LeagueLinks> _links { get; set; }
    public string count { get; set; }
    public IDictionary<string, LeagueTeam> teams { get; set; }
}

public class LeagueTeam
{
    public IDictionary<string, TeamLinks> _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; }
}
public class TeamLinks
{
    public string href { get; set; }
}

      

But I am getting the following error:

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll Additional Information: Unable to deserialize the current JSON array (eg [1,2,3]) to type System.Collections.Generic.IDictionary`2 [System. String, FootballTestApplication.LeagueTeam] 'because this type requires a JSON object (eg {"name": "value"}) to deserialize correctly.

When I look at what the api gives me for the response, I see a difference in how the response starts. When collecting all leagues, it starts and ends with brackets ([]), but when collecting teams in a league, it does not start or end with brackets. When I add the parentheses, I still get the error. This is how I add the parentheses:

return JsonConvert.DeserializeObject<List<LeagueTeams>>("["+content+"]");

      

What am I doing wrong here?

+3


source to share


1 answer


The problem is that the returned JSON is the NOT a Array

, List

, Dictionary

or any other Enumerable

, but instead is object

.

If we take your JSON from this API link and step through item by item, we know yours is Type

wrong.

You are telling the JSON Serializer what the root object is List<LeagueTeams>

, but it is not. This is actually the only object LeagueTeams

.

If we make this deserializer modification, and we make a couple of modifications to your class LeagueTeams

(basically the fact that you are deserializing the property links

wrong), you're all set:

public class LeagueTeams
{
    public List<IDictionary<string, string>> _links { get; set; }
    public string count { get; set; }
    public List<LeagueTeam> teams { get; set; }
}

return JsonConvert.DeserializeObject<LeagueTeams>(content);

      



I have attached an image of the results.

Inspection of your Object

Additional Notes

One more note: the C # JSON site ( http://json2csharp.com/ ) does NOT handle this JSON correctly, it actually generates an object that is almost correct, but not quite. (Although the generated object will still work correctly, this is not the correct display of the input JSON.) This is due, in part, to a generator error, and also to the website. It tries to generate a list of two properties Link

for the root elementlinks

where instead he had to use a dictionary list to get the correct collations. (Although this is a bit dumb, this is the most correct approach.) Another thing to consider, yes, it's a good place to start, but generally correct JSON handling requires more control over the generated objects to ensure correctness.

+1


source







All Articles