3

I want to parse JSON to list with Flurl. My JSON data like this.

{
  "api": {
    "results": 114,
    "fixtures": {
      "195": {
        "fixture_id": "195",
        "event_timestamp": "1543759500",
        "event_date": "2018-12-02T14:05:00+00:00",
        "league_id": "2",
        "round": "Premier League - 14",
        "homeTeam_id": "42",
        "awayTeam_id": "47",
        "homeTeam": "Arsenal",
        "awayTeam": "Tottenham",
        "status": "Match Finished",
        "statusShort": "FT",
        "goalsHomeTeam": "4",
        "goalsAwayTeam": "2",
        "halftime_score": "1 - 2",
        "final_score": "4 - 2",
        "penalty": null,
        "elapsed": "87",
        "firstHalfStart": "1543759500",
        "secondHalfStart": "1543763100"
      }
}}}

and I have also class like this.

class Fixture
    {
        //public string results { get; set; }
        public string fixture_id { get; set; }
        public string event_timestamp { get; set; }
        public string event_date { get; set; }
        public string league_id { get; set; }
        public string round { get; set; }
        public string homeTeam_id { get; set; }
        public string awayTeam_id { get; set; }
        public string homeTeam { get; set; }
        public string awayTeam { get; set; }
        public string status { get; set; }
        public string statusShort { get; set; }
        public string goalsHomeTeam { get; set; }
        public string goalsAwayTeam { get; set; }
        public string halftime_score { get; set; }
        public string final_score { get; set; }
        public string penalty { get; set; }
        public string elapsed { get; set; }
        public string firstHalfStart { get; set; }
        public string secondHalfStart { get; set; }
    }

My code is on below

            try
            {
                string _url = string.Format("https://api-football-v1.p.mashape.com/fixtures/date/{0}",(DateTime.Now.Year+"-"+DateTime.Now.Month+"-"+DateTime.Now.Day).ToString());
                //PERFORM IN BACKGROUND
                await Task.Run(async () =>
                {
                   var fixtures= await _url.WithHeader("Accept", "application/json").WithHeader("X-Mashape-Key", "g5vnMIqeChmshvK6H25VPavNCfhHp1KUtTkjsnOqEM06eP6cOd").GetJsonAsync<Fixture>();
                });
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

I try to get JSON data with Flurl.HTTP. But I have deserialized problem. Where is my mistake?

How can I parse this JSON data to IList in c#?

Thank you for your support friends...

  • What exactly is the error message of your exception? Your description "I have deserialized problem" is not clear enough. – Karel Kral Dec 05 '18 at 10:29

1 Answers1

2

You're basically trying to select an inner section of the JSON response, and you can't do that - you need to deserialize to a structure representing the entire response, then traverse this structure to get an individual Fixture. Looking at full response body, you will need to add 2 more classes to contain this structure:

public class ResponseBody
{
    public Api api { get; set; }
}

public class Api
{
    public int results { get; set; }
    public Dictionary<string, Fixture> fixtures { get; set; }
}

Then call GetJsonAsync<ResponseBody>() instead of GetJsonAsync<Fixture>().

Todd Menier
  • 37,557
  • 17
  • 150
  • 173