使用Flurl的反序列化问题GetJsonAsync <t>

时间:2018-12-03 21:06:44

标签: flurl

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

listBox1.SelectedValue

我也有这样的课程。

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

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

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

谢谢您的支持朋友...

1 个答案:

答案 0 :(得分:0)

您基本上是在尝试选择JSON响应的内部部分,而您不能这样做-您需要反序列化为表示 entire 响应的结构,然后遍历该结构以得到一个个人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>()