繁体   English   中英

C#ASP.NET JSON到JSON数组无法反序列化当前JSON对象

[英]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。

我的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
}

或者如果您需要保留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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM