[英]C# ASP.NET JSON to a JSON array Cannot deserialize the current JSON object
我正在使用Json.Net將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]),也可以更改反序列化類型,使其成為普通的.NET類型(例如,不像整數這樣的原始類型,也不像這樣的集合類型)數組或列表),可以從JSON對象反序列化。 還可以將JsonObjectAttribute添加到類型中,以強制其從JSON對象反序列化。 路徑“ summonerId”,第1行,位置14。
我的課
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
}
或者如果您需要保留GetRecipes方法的原型:
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.