3

我想解析 JSON 以使用 Flurl 列出。我的 JSON 数据是这样的。

{
  "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"
      }
}}}

我也有这样的课。

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; }
    }

我的代码在下面

            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);
            }

我尝试使用 Flurl.HTTP 获取 JSON 数据。但我有反序列化的问题。我的错误在哪里?

如何在 C# 中将此 JSON 数据解析为 IList?

谢谢各位朋友的支持...

4

1 回答 1

2

您基本上是在尝试选择 JSON 响应的内部部分,但您不能这样做 - 您需要反序列化为表示整个响应的结构,然后遍历该结构以获取单个Fixture. 查看完整的响应正文,您需要再添加 2 个类来包含此结构:

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

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

然后调用GetJsonAsync<ResponseBody>()而不是GetJsonAsync<Fixture>().

于 2018-12-10T18:10:08.290 回答