flurl - 反序列化问题 GetJsonAsync<T> with Flurl

标签 flurl

我想解析 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?

谢谢 friend 们的支持...

最佳答案

您基本上是在尝试选择 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>() .

关于flurl - 反序列化问题 GetJsonAsync<T> with Flurl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53601866/

相关文章:

c# - 使用 Flurl 发布 `multipart/form-data`

flurl - 如何使用 Flurl v3.0.0 自动记录响应

http - ASP.Net Core 2.1 和 IHttpClientFactory 中的 Flurl 客户端生命周期

c# - Flurl 的 PostUrlEncodedAsync 忽略 JsonProperty 名称

c# - FLURL:映射属性名称

C# HttpClient tor socks4/5 代理?

c# - 如何将 Polly 与 Flurl.Http 一起使用?

c# - 如何更改 FLURL 客户端的 HTTP 请求内容类型?

c# - 使用 Flurl 设置默认的 Polly 策略