C# 将 JSON ASP.NET JSON 到 JSON 数组 无法反序列化当前 JSON 对象
本文关键字:JSON 反序列化 对象 数组 ASP NET | 更新日期: 2023-09-27 18:33:41
我正在使用 Json.Net 将Object Json数据反序列化为对象/集合(列表(
我不确定我做错了什么,我已经尝试过这个:
public List<RootObject> GetRecipes()
{
string summonerID = Session["summonerID"].ToString();
string downloadedString;
WebClient client = new WebClient();
downloadedString = client.DownloadString("https://tr.api.pvp.net/api/lol/tr/v1.3/game/by-summoner/" + summonerID + "/recent?api_key=55686aef-da52-4184-b987-98799662d92e"); //tırnak içerisine istediğiniz web adresini yazınız
List<RootObject> liste = JsonConvert.DeserializeObject<List<RootObject>>(@downloadedString);
return liste;
lblYazi.Text = liste.ToString();
}
我收到此错误:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[KYLOL2.WebForm2+RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
若要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3](或更改反序列化类型,使其是可以从 JSON 对象反序列化的普通 .NET 类型(例如,不是整数等基元类型,也不是数组或列表等集合类型(。还可以将 JsonObjectAttribute 添加到类型中,以强制它从 JSON 对象反序列化。路径"召唤师 ID",第 1 行,位置 14。
我的 Json 链接
我的班级
public class FellowPlayer
{
public int summonerId { get; set; }
public int teamId { get; set; }
public int championId { get; set; }
}
public class Stats
{
public int level { get; set; }
public int goldEarned { get; set; }
public int numDeaths { get; set; }
public int minionsKilled { get; set; }
public int championsKilled { get; set; }
public int goldSpent { get; set; }
public int totalDamageDealt { get; set; }
public int totalDamageTaken { get; set; }
public int doubleKills { get; set; }
public int killingSprees { get; set; }
public int largestKillingSpree { get; set; }
public int team { get; set; }
public bool win { get; set; }
public int neutralMinionsKilled { get; set; }
public int largestMultiKill { get; set; }
public int physicalDamageDealtPlayer { get; set; }
public int magicDamageDealtPlayer { get; set; }
public int physicalDamageTaken { get; set; }
public int magicDamageTaken { get; set; }
public int largestCriticalStrike { get; set; }
public int timePlayed { get; set; }
public int totalHeal { get; set; }
public int totalUnitsHealed { get; set; }
public int assists { get; set; }
public int item0 { get; set; }
public int item1 { get; set; }
public int item2 { get; set; }
public int item3 { get; set; }
public int item4 { get; set; }
public int item6 { get; set; }
public int magicDamageDealtToChampions { get; set; }
public int physicalDamageDealtToChampions { get; set; }
public int totalDamageDealtToChampions { get; set; }
public int trueDamageTaken { get; set; }
public int neutralMinionsKilledEnemyJungle { get; set; }
public int totalTimeCrowdControlDealt { get; set; }
public int playerRole { get; set; }
public int playerPosition { get; set; }
public int? turretsKilled { get; set; }
public int? item5 { get; set; }
public int? wardPlaced { get; set; }
public int? neutralMinionsKilledYourJungle { get; set; }
public int? trueDamageDealtPlayer { get; set; }
public int? trueDamageDealtToChampions { get; set; }
public int? bountyLevel { get; set; }
public int? tripleKills { get; set; }
public int? wardKilled { get; set; }
public int? barracksKilled { get; set; }
}
public class Game
{
public int gameId { get; set; }
public bool invalid { get; set; }
public string gameMode { get; set; }
public string gameType { get; set; }
public string subType { get; set; }
public int mapId { get; set; }
public int teamId { get; set; }
public int championId { get; set; }
public int spell1 { get; set; }
public int spell2 { get; set; }
public int level { get; set; }
public int ipEarned { get; set; }
public object createDate { get; set; }
public List<FellowPlayer> fellowPlayers { get; set; }
public Stats stats { get; set; }
}
public class RootObject
{
public int summonerId { get; set; }
public List<Game> games { get; set; }
}
您告诉反序列化程序将 RootObject 作为列表查找,但它不在你提供的示例中。 像这样的东西应该更好用:
RootObject liste = JsonConvert.DeserializeObject<RootObject>(@downloadedString);
所以像这样:
public RootObject GetRecipes()
{
string summonerID = Session["summonerID"].ToString();
string downloadedString;
WebClient client = new WebClient();
downloadedString = client.DownloadString("https://tr.api.pvp.net/api/lol/tr/v1.3/game/by-summoner/" + summonerID + "/recent?api_key=55686aef-da52-4184-b987-98799662d92e"); //tırnak içerisine istediğiniz web adresini yazınız
RootObject liste = JsonConvert.DeserializeObject<RootObject>(@downloadedString);
return liste;
lblYazi.Text = liste.ToString(); // never reaches this code
}
或者,如果您需要保留 GetRerecies 方法的原型:
public List<RootObject> GetRecipes()
{
string summonerID = Session["summonerID"].ToString();
string downloadedString;
WebClient client = new WebClient();
downloadedString = client.DownloadString("https://tr.api.pvp.net/api/lol/tr/v1.3/game/by-summoner/" + summonerID + "/recent?api_key=55686aef-da52-4184-b987-98799662d92e"); //tırnak içerisine istediğiniz web adresini yazınız
List<RootObject> liste = new List<RootObject>(); // create the return list
RootObject e = JsonConvert.DeserializeObject<RootObject>(@downloadedString);
liste.Add(e); // add the reserialized object to the list we are returning
return liste;
lblYazi.Text = liste.ToString(); // never reaches this code
}