在c#中从具有多个对象的字符串反序列化JSON对象



我一直在尝试反序列化来自网页的JSON文件,然后将其放入列表中。代码如下:

public async Task Update() {
try{
HttpResponseMessage response = await client.GetAsync("https://understat.com/league/Serie_A");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
string responseParsed = Regex.Replace(responseBody, @"\x[0-9a-fA-Z]{2}", HexConvert.DecodeHex);
string[] sub1 = responseParsed.Split(@"<script>");
string sub2 = sub1[4].Substring(33);
string teamData = sub2.Split(@"')")[0];
AllPlayers = JsonSerializer.Deserialize<List<Player>>(teamData);
foreach (Player p in AllPlayers) Console.WriteLine(p.Player_Name + "n");
} catch(HttpRequestException e) {
Console.WriteLine("nException Caught!");   
Console.WriteLine("Message :{0} ",e.Message);
}
}

Class Player是:

public class Player{
public string Id { get ; set; }
public string Player_Name { get; set; }
public string Games { get; set; }
public string Time { get; set; }
public string Goals { get; set; }
public string xG { get; set; }
public string Assists { get; set ; }
public string xA { get; set; }
public string Shots { get; set; }
public string Key_Passes { get; set; }
public string Yellow_Cards { get ; set; }
public string Red_Cards { get; set; }
public string Position { get; set; }
public string Team_Title { get; set; }
public string nPG { get; set; }
public string npxG { get; set; }
public string xGChain { get; set; }
public string xGBuildup { get; set; }
public int Cost { get; set; }
public int YearsOfContract { get; set; }
//public int AvgVote { get; set; }
//public Club Squad { get; set; }
[JsonConstructor]
public Player(string id, string player_name, string games, string time, string goals, string xg, string assists, string xa, 
string shots, string key_passes, string yellow_cards, string red_cards, string position, string team_title, string npg, string npxg, string xgchain, string xgbuildup) {
Id = id;
Player_Name = player_name;
Games = games;
Time = time;
Goals = goals;
xG = xg;
Assists = assists;
xA = xa;
Shots = shots;
Key_Passes = key_passes;
Yellow_Cards = yellow_cards;
Red_Cards = red_cards;
Position = position;
Team_Title = team_title;
nPG = npg;
npxG = npxg;
xGChain = xgchain;
xGBuildup = xgbuildup;
Cost = cost;
YearsOfContract = yearsofcontract;
}
public int GetRenewalCost(){
return (Cost * (int)Math.Ceiling(0.3 + YearsOfContract*0.1));
}
public int GetRidCost(){
return (Cost* (int)Math.Ceiling(YearsOfContract*0.1));
}
}
最后,JSON字符串看起来像这样(原谅图片,但它太长了,无法复制和粘贴):

JSON字符串

然而,当反序列化时,我得到一个System.Text.Json.JsonException。知道为什么吗?
Message: The JSON value could not be converted to System.Collections.Generic.List`1[Players.Player]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.

提前感谢,如果需要进一步的代码,我将非常乐意添加它!

我实际上设法通过拆分各种条目然后逐个反序列化它们到Player字段来修复它。结果类最终如下所示:

public async Task Update() {
try{
HttpResponseMessage response = await client.GetAsync("https://understat.com/league/Serie_A");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
string responseParsed = Regex.Replace(responseBody, @"\x[0-9a-fA-Z]{2}", HexConvert.DecodeHex);
string[] sub1 = responseParsed.Split(@"<script>");
string sub2 = sub1[4].Substring(33);
string playersData = sub2.Split(@"')")[0];
string[] allPlayers = playersData.Split(@"},");
for(int i=0; i<allPlayers.Length-1; i++) allPlayers[i] += '}';
allPlayers[allPlayers.Length-1] = allPlayers[allPlayers.Length-1].Substring(0, allPlayers[allPlayers.Length-1].Length-1);
for(int i=0; i<allPlayers.Length; i++) AllPlayers.Add(JsonSerializer.Deserialize<Player>(allPlayers[i]));
for(int i=0; i<AllPlayers.Count; i++) Console.WriteLine(AllPlayers[i].PrintPlayer());
} catch(HttpRequestException e) {
Console.WriteLine("nException Caught!");   
Console.WriteLine("Message :" + e.Message);
} catch(JsonException e){
Console.WriteLine("nException Caught!");
Console.WriteLine("Message: " + e.Message);
}
}

相关内容

  • 没有找到相关文章

最新更新