gpt4 book ai didi

flurl - 反序列化问题 GetJsonAsync with Flurl

转载 作者:行者123 更新时间:2023-12-04 15:51:30 30 4
gpt4 key购买 nike

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

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com